使用Junit Vintage Engine运行Junit 3测试
在我们的项目中,我们有很多Junit 3测试看起来像:
@RunWith(AllTests.class)
public class JUnit3Test extends TestCase {
public static TestSuite suite()
{
var suite = new TestSuite("JUnit 3 style test suite");
suite.addTest(new JUnit3Test("JUnit 3 style test") {
@Override
protected void runTest() {
testSomething();
}
});
return suite;
}
public void testSomething() {
...
}
...
}
当前,我们正在使用Junit 4执行它们,但是我正在尝试转到Junit 5。在Gradle构建文件中,这些测试不再执行。
这些更改中的任何一个将成功执行Junit 3测试:
- 切换到Junit 4-
USEJUNIT()
而不是usejunitplatform()
。 - 将测试的名称从
junit 3样式测试
更改为testsomelibrarymethoderturnstrunstrue
。 - 删除
@runwith(alltests.class)
注释和suite()
方法。
我不知道这个示例甚至应该在Junit 5中起作用,似乎没有很多文档在使用Junit 5进行Junit 3测试时。测试在suite()
方法中做各种事情。
In our project we have quite a few JUnit 3 tests looking like:
@RunWith(AllTests.class)
public class JUnit3Test extends TestCase {
public static TestSuite suite()
{
var suite = new TestSuite("JUnit 3 style test suite");
suite.addTest(new JUnit3Test("JUnit 3 style test") {
@Override
protected void runTest() {
testSomething();
}
});
return suite;
}
public void testSomething() {
...
}
...
}
Currently, we're executing them using JUnit 4, but I'm trying to move to JUnit 5. When I change to useJUnitPlatform()
in the Gradle build file these tests are no longer executed.
There's a full working example in https://github.com/ulrikls/junit-vintage-alltests
Any of these changes will successfully execute the JUnit 3 test:
- Switching to JUnit 4 -
useJUnit()
instead ofuseJUnitPlatform()
. - Changing the name of the test from
JUnit 3 style test
totestSomeLibraryMethodReturnsTrue
. - Removing the
@RunWith(AllTests.class)
annotation and thesuite()
method.
I don't know if this example is even supposed to work in JUnit 5, there doesn't seem to be a lot of documentation on running JUnit 3 tests with JUnit 5. But migrating all our JUnit 3 tests is also not trivial, as the tests do various stuff in the suite()
method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论