JUnit - 指定测试对其他测试的依赖关系(不跳过任何一个)?
我们的工具包有超过 15000 个 JUnit 测试,并且已知如果其他测试失败,许多测试也会失败。例如,如果方法 X.foo() 使用 Y.bar() 的功能并且 YTest.testBar() 失败,则 XTest.testFoo() 也会失败。显然,XTest.testFoo() 也可能因为 X.foo() 特有的问题而失败。
虽然这很好,而且我仍然希望两个测试都运行,但如果可以用指向 YTest.testBar() 的 XTest.testFoo() 注释测试依赖项,那就太好了。这样,人们可以立即看到 X.foo() 使用的哪些功能也失败了,哪些没有失败。
JUnit 或其他地方有这样的注释吗?像这样的东西:
public XTest {
@Test
@DependsOn(method=org.example.tests.YTest#testBar)
public void testFoo() {
// Assert.something();
}
}
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 YTest.testBar() fails, then XTest.testFoo() will fail too. Obviously, XTest.testFoo() can also fail because of problems specific to 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.
Is there such annotation available in JUnit or elsewhere? Something like:
public XTest {
@Test
@DependsOn(method=org.example.tests.YTest#testBar)
public void testFoo() {
// Assert.something();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
JUnit 还不能(在撰写本文时,2022 年)。
但是 JExample 和 TestNG 有类似的东西。
我不知道它有多有用,但是如果您尝试一下,请回来告诉我们它是否有用。
JUnit simply can't yet (at time of writting, 2022).
But both 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.
JUnit 的一个贡献解决了这个问题: https://github.com /junit-team/junit.contrib/tree/master/assumes
Update 2022; Assumes 现已发布,但这只是跳过测试,这不是 OP 所要求的。
There's a contribution to JUnit that address this: https://github.com/junit-team/junit.contrib/tree/master/assumes
Update 2022; Assumes is out by now, but that only skips tests, which is not what OP asks for.
org.junit.FixMethodOrder
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
这位于您的单元测试类之上。
您可以将方法命名为 public void step1_methodName 等
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
您可以在 TestNG 中声明 测试依赖项,语法与示例中的语法几乎相同。我不认为 JUnit 提供类似的东西。
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.
在行为驱动设计库jBehave 有一个关键字
GivenScenarios
,它导入在主场景之前运行的场景列表。这提供了定义依赖项并设置单点故障的机会。 jBehave 的日志记录会告诉您依赖项或主体部分的测试是否失败。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.我确实没有意识到这样的事情。(编辑:你每天都会学到新东西:))在我看来,这并不是一件坏事(尽管我可以看到它很有用,特别是当 JUnit 用于其他形式的自动化测试(例如集成测试)时。 IMO,您的测试并不是最严格意义上的“单元测试”(至少不是X#foo()
的测试)。X#foo()
的测试应该成功或失败,仅取决于X#foo()
的实现。它不应依赖于Y#foo()
。我在你的位置上要做的就是模拟 Y,并以非常简单的受控行为实现类似
MockY#foo()
的东西,并在X#foo()< 中使用它/code> 的测试。
也就是说,通过 15,000 次测试,我可以看出重构是多么痛苦。 :)
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 forX#foo()
). Tests forX#foo()
should succeed or fail depending only on the implementation ofX#foo()
. It should not be dependent onY#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 inX#foo()
's tests.That said, with 15,000 tests, I can see how this would be a pain to refactor. :)