Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit tests in eclipse failing when run together

Tags:

junit

eclipse

I have a set of 44 JUnit tests that I run using Eclipse (I got those tests from someone else - I am new to JUnit tests). When I run them all together, 24 of them fail. However, if I then run the failing ones individually, some of them do pass after all. The tests do take a little time - one of the failing ones for example takes about one or two minutes to complete, while just letting all of them run finishes in just a few seconds.

I am starting multiple tests by right-clicking on the folder they are in and selecting "Run As -> JUnit Test". I am using JUnit 3. Am I doing something wrong in starting them / is there some kind of option I am missing?

like image 500
pyvi Avatar asked Nov 26 '10 15:11

pyvi


People also ask

Why is my JUnit test failing?

When writing unit tests with JUnit, there will likely be situations when tests fail. One possibility is that our code does not meet its test criteria. That means one or more test cases fail due to assertions not being fulfilled.

Is parallel execution possible in JUnit?

By enabling parallel execution, the JUnit engine starts using the ForkJoin thread pool. Next, we need to add a configuration to utilize this thread pool. We need to choose a parallelization strategy. JUnit provides two implementations (dynamic and fixed) and a custom option to create our implementation.

Does JUnit 4 run tests in parallel?

A plugin that allows you to run JUnit4 tests in parallel (using multiple CPU cores/threads).

Can we have both JUnit and TestNG together?

All you have to do is to put JUnit library on the TestNG classpath, so it can find and use JUnit classes, change your test runner from JUnit to TestNG in Ant, and then run TestNG in "mixed" mode. This way, you can have all your tests in the same project, even in the same package, and start using TestNG.


2 Answers

It's hard to say for sure without seeing the tests, but it sounds to me like they share some state or resource that is not being reset correctly for the next test.

like image 111
GaryF Avatar answered Oct 03 '22 23:10

GaryF


GaryF's answer is one possibility. Another is that the tests have a race-condition: whether or not a test succeeds depends on how fast something occurs (which can vary due to the vagaries of the O/S). If you run the failing tests separately, do they always succeed or do they sometimes fail. If they sometimes fail, then you likely have a race-condition.

like image 31
Steve Emmerson Avatar answered Oct 04 '22 00:10

Steve Emmerson