Java 如何指定 JUnit 测试依赖项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2584629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I specify JUnit test dependencies?
提问by Egon Willighagen
Our toolkit has over 15000 JUnittests, 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 YTest.testBar() fails, then XTest.testFoo() will fail too. Obviously, XTest.testFoo() can also fail because of problems specific to X.foo().
我们的工具包有超过 15000 个JUnit测试,如果其他测试失败,许多测试就会失败。例如,如果方法 X.foo() 使用来自 Y.bar() 的功能并且 YTest.testBar() 失败,那么 XTest.testFoo() 也会失败。显然,XTest.testFoo() 也可能因为 X.foo() 特有的问题而失败。
While this is fine and I still want both tests run, it would be nice if one could annotate a test dependency with XTest.testFoo() pointing to YTest.testBar(). This way, one could immediately see what functionality used by X.foo() is also failing, and what not.
虽然这很好,而且我仍然希望运行两个测试,但如果可以使用 XTest.testFoo() 指向 YTest.testBar() 来注释测试依赖项,那就太好了。通过这种方式,人们可以立即看到 X.foo() 使用的哪些功能也失败了,哪些没有。
Is there such annotation available in JUnit or elsewhere? Something like:
JUnit 或其他地方是否有这样的注释?就像是:
public XTest {
@Test
@DependsOn(method=org.example.tests.YTest#testBar)
public void testFoo() {
// Assert.something();
}
}
采纳答案by Esko Luontola
回答by Hyman Leow
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 onlyon the implementation of X#foo()
. It should not be dependent on Y#foo()
.
真的没有这样的事情,我知道。(编辑:你每天都在学习新东西:))在我看来,这并不是一件坏事(尽管我认为它很有用,尤其是当 JUnit 用于其他形式的自动化测试时——例如,集成测试)。你的测试,IMO,不是“单元测试”这个词最严格的意义(至少不是测试X#foo()
)。用于测试X#foo()
应该会成功或失败取决于仅在执行X#foo()
。它不应该依赖于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.
在你的位置上,我会做的是模拟 Y,并MockY#foo()
用非常简单的、受控的行为来实现类似的东西,并在X#foo()
的测试中使用它。
That said, with 15,000 tests, I can see how this would be a pain to refactor. :)
也就是说,通过 15,000 次测试,我可以看出重构是多么痛苦。:)
回答by Thomas L?tzer
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.
您可以在 TestNG 中声明测试依赖项,语法与您的示例中几乎相同。我不认为 JUnit 提供类似的东西。
回答by Boris Pavlovi?
In behavior driven designlibrary jBehavethere'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.
在行为驱动的设计库jBehave 中有一个关键字GivenScenarios
,用于导入在主场景之前运行的场景列表。这提供了定义依赖项并有一个故障点的机会。jBehave 的日志记录会告诉您测试是否在依赖项或主体部分失败。
回答by Jose Muanis
There's a contribution to JUnit that address this: https://github.com/junit-team/junit.contrib/tree/master/assumes
有一个对 JUnit 的贡献解决了这个问题:https: //github.com/junit-team/junit.contrib/tree/master/assumes
回答by Aditya Digumarti
org.junit.FixMethodOrder
org.junit.FixMethodOrder
@FixMethodOrder(MethodSorters.NAME_ASCENDING) This goes on top of your Unit test class.
@FixMethodOrder(MethodSorters.NAME_ASCENDING) 这在你的单元测试类之上。
You can name your methods public void step1_methodName etc
您可以将您的方法命名为 public void step1_methodName 等