Junit 4 test suite and individual test classes

I have a JUnit 4 test suite with BeforeClass and AfterClass methods that do install / disable for the following test classes. What I need is to run the test classes by myself, but for this I need a setup / break script (BeforeClass and AfterClass or something like that) for each test class. The fact is that when I run the package, I don’t want to configure / disconnect before and after each test class, I only want to configure / disconnect from the test suite (once). Is it possible? Thanks in advance.

+6
java unit-testing junit junit4
source share
3 answers

I don't know of any standard way to do this with JUnit. The reason for this, as you probably already know, is that your test cases should run independently. This applies to the “normal” setup / break methods that are performed before and after each test method. Class setup and decompression are slightly different, though, although I would prefer to run my tests myself and stay out of the problem zone.

However, if you are really sure of what you are doing, you can use the global flag to signal whether the class should be started / disabled, and check its status in the class settings / deletion methods. In your test case, you can include a special class as the very first one, which does nothing more than configure and sets a global flag to show real test cases that their class installation / removal methods should not be executed. Similarly, the special last class in the package can execute break code. The bottom line is that I'm afraid that JUnit does not guarantee the execution order of test classes inside the package, although most likely it will execute them in the specified order, but this is just an implementation detail. Try it, it may work for you - but there is no guarantee that he will always do what you expect.

+4
source share

If you have jUnit 4.7+, I recommend a peek at a new feature called "Rules" (which are described in this blog post ). They may not be exactly what you want, but they are probably best suited for jUnit.

TestNG supposedly has the best testing capabilities, but I haven't explored it myself yet.

+3
source share

No, there is no standard way to do this in JUnit, although you could hack something, as Peter Turok suggested.

Note, however, that you are more or less abusing JUnit in doing so. The whole unit of measurement verifies that they are independent of each other. This is because the dependencies between the tests create a common service nightmare (the tests fail because the launch is performed in the wrong order).

Therefore, I would advise you to seriously think about whether it is better to just always run the setup ...

0
source share

All Articles