How can I specify JUnit test dependencies?

后端 未结 6 1111
南方客
南方客 2020-12-23 09:37

Our toolkit has over 15000 JUnit tests, and many tests are known to fail if some other test fails. For example, if the method X.foo() uses functionality from Y.bar() and YTe

相关标签:
6条回答
  • 2020-12-23 09:49

    In behavior driven design library jBehave there's a keyword GivenScenarios which imports a list of scenarios that are run before the main scenario. This gives an opportunity to define dependencies and have one point of failure. jBehave's logging will tell you if test fails in dependencies or main body section.

    0 讨论(0)
  • 2020-12-23 09:52

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

    0 讨论(0)
  • 2020-12-23 10:02

    There really isn't something like this that I'm aware of. (Edit: you learn something new every day :)) In my opinion, this isn't that bad of a thing (though I can see it being useful, especially when JUnit it being used for other forms of automated tests - e.g., integration tests). Your tests, IMO, aren't in the strictest sense of the word "unit tests" (at least not the test for X#foo()). Tests for X#foo() should succeed or fail depending only on the implementation of X#foo(). It should not be dependent on Y#foo().

    What I'd do in your position is to mock out Y, and implement something like MockY#foo() with very simple, controlled behavior, and use it in X#foo()'s tests.

    That said, with 15,000 tests, I can see how this would be a pain to refactor. :)

    0 讨论(0)
  • 2020-12-23 10:04

    JExample and TestNG have something like that.

    I don't know how useful it is, but if you try it, please come back to tell us whether it was useful.

    0 讨论(0)
  • 2020-12-23 10:04

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

    0 讨论(0)
  • 2020-12-23 10:06

    org.junit.FixMethodOrder

    @FixMethodOrder(MethodSorters.NAME_ASCENDING) This goes on top of your Unit test class.

    You can name your methods public void step1_methodName etc

    0 讨论(0)
提交回复
热议问题