Get JUnit to run one test case at a time

I have a problem situation with some pretty advanced unit tests (using PowerMock for bullying and JUnit 4.5). Without going into details, the first test case of a test class will always be successful, but any subsequent test cases in the same test class will fail. However, if I choose to only run a test case of 5 out of 10, for example, it will pass. Thus, all tests pass at startup separately. Is there a way to get JUnit to run one test case at a time? I am calling JUnit from ant - script.

I know the problem of dependent test cases, but I cannot pinpoint why this is so. In test cases, there are no stored variables, so nothing should be done in the @Before annotation. This is why I am looking for an emergency solution, such as forcing JUnit to run separate tests.

+6
java unit-testing junit testing ant
source share
9 answers

Your problem is not that JUnit runs all the tests at once, the problem is that you do not see why the test fails. Solutions:

  • Add more statements to the tests to make sure that each variable contains, in your opinion,
  • Download the IDE from the Internet and use the built-in debugger to look at various variables
  • Reset the state of your objects immediately before the point at which the test failed.
  • Use the “message” part of the claims section to display additional information about why it does not work (see below).
  • Disable all but a few tests (in JUnit 3: replace all lines of "void test" with "void dtest" in your source, in JUnit 4: replace "@Test" with "// D @TEST").

Example:

assertEquals(list.toString(), 5, list.size()); 
+5
source share

I know all the recommendations, but finally, answering your question here is an easy way to achieve what you want. Just enter this code in your test case:

 Lock sequential = new ReentrantLock(); @Override protected void setUp() throws Exception { super.setUp(); sequential.lock(); } @Override protected void tearDown() throws Exception { sequential.unlock(); super.tearDown(); } 

However, no test can be run until a lock is received, and only one lock can be obtained at a time.

+12
source share

It seems your test cases are dependent, that is: case-X execution affects case-Y execution. Such a testing system should be avoided (for example: there is no guarantee on which JUnit your case will work on).

You must reorganize your affairs to make them independent of each other. Many times using the @Before and @After methods can help you unravel such dependencies.

+9
source share

Congratulations. You found a mistake .; -)

If the tests "should not" influence each other, then you may have discovered a situation where your code may enter the "broken" state. Try adding assertions and logging to find out where the code goes wrong. You might even need to run tests in the debugger and check the internal code values ​​after the first test.

+3
source share

Sorry if I don’t answer your question directly, but isn’t your problem that those TestCase.setUp () and TestCase.tearDown () tests should solve? These are methods that the JUnit framework will always call before and after each test case and are commonly used to ensure that each test case runs in the same state.

See also JavaDoc for TestCase.

+2
source share

You should check your entire codebase that there are no static variables that relate to mutable state. Ideally, the program should not have a static mutable state (or at least they should be documented as I did here ). You should also be very careful to clear what you write if tests are written to the file system or database. Otherwise, running the tests can cause some side effects, which makes the independence and repeatability of the tests more difficult.

Maven and Ant contain a forkmode parameter for running JUnit tests, which determines whether each test class will receive its own JVM or all tests run in the same JVM. But they don’t have the ability to run each test method in their own JVM.

+1
source share

Your description shows me that your unit tests depend on each other. Unit tests are strongly discouraged .

Unit test must be independent and isolated. You should be able to execute them alone, all of them (in what order it does not matter).

I know it won’t help you. The problem will be with your @BeforeClass or @Before . There are dependencies. So reorganize them and try to isolate the problem.

Your layouts are probably being created in your @BeforeClass . Consider putting it in the @Before . Thus, there is no instance that lasts longer than the test case.

0
source share

I know about the problem of dependent test cases, but I can’t determine exactly why this is. There are no saved variables through test cases, so do nothing in @ Before annotating. That's why I'm looking for an emergency solution on how to get JUnit to run tests separately.

The @Before statement is harmless because it is called for each test case. Class @Before The class is dangerous because it must be static.

0
source share

It seems to me that perhaps this is not that you are not setting up or breaking your tests properly (although additional tuning / detachment may be part of the solution), but perhaps you have a general state in the code, you do not know. If an early test sets a static / single / shared variable that you don't know about, later tests will fail if they don't expect it. Even with Mocks, this is very possible. You need to find this reason. I agree with the other answers that your tests have identified a problem that should not be resolved by trying to run the tests in different ways.

0
source share

All Articles