How can I specify JUnit test dependencies?

Our toolkit has over 15,000 JUnit tests, and many tests are known to fail if any other test fails. For example, if the X.foo () method uses the functionality from Y.bar () and YTest.testBar (), then XTest.testFoo () will not work. Obviously, XTest.testFoo () may also fail due to problems specific to X.foo ().

While this is normal, and I still want both tests to run, it would be nice if you could annotate the test's dependency on XTest.testFoo (), pointing to YTest.testBar (). Thus, you can immediately see which functionality used by X.foo () also fails and what does not.

Is there such an annotation in JUnit or elsewhere? Something like:

public XTest { @Test @DependsOn(method=org.example.tests.YTest#testBar) public void testFoo() { // Assert.something(); } } 
+46
java unit-testing junit
Apr 6 2018-10-06T00:
source share
6 answers

JExample and TestNG have something like this.

I do not know how useful this is, but if you try, please come back to let us know if it is useful.

+22
Apr 6 2018-10-12T00:
source share

There is a contribution to JUnit that addresses this: https://github.com/junit-team/junit.contrib/tree/master/assumes

+17
Oct 28 2018-11-11T00:
source share

You can declare test dependencies in TestNG , the syntax is almost the same as in your example. I don't think JUnit offers something like this.

+3
Apr 6 '10 at 12:12
source share

org.junit.FixMethodOrder

@FixMethodOrder (MethodSorters.NAME_ASCENDING) This belongs to the Unit test class.

You can name your methods public void step1_methodName, etc.

+3
Dec 26 '14 at 7:27
source share

Actually, this is not so, that I know. (Edit: you learn something new every day :)) In my opinion, this is not so bad (although I see that it is useful, especially when JUnit is used for other forms of automatic tests - for example, integration tests). Your tests, IMO, are not in the strict sense of the word "unit tests" (at least not a test for X#foo() ). Tests for X#foo() should succeed or fail depending on the implementation of X#foo() . It should not depend on Y#foo() .

What I would do in your post is to mock Y and implement something like MockY#foo() with a very simple controlled behavior and use it in X#foo() tests.

However, with 15,000 tests, I see how this will be a pain for refactoring. :)

+1
Apr 6 2018-10-06T00:
source share

The jBehave behavior-driven library has a GivenScenarios keyword that imports a list of scripts that run before the main script. This makes it possible to determine the dependencies and have one point of failure. jBehave logging will tell you if the test fails in the dependencies or section of the main body.

+1
Apr 6 '10 at 12:15
source share



All Articles