使用Junit Vintage Engine运行Junit 3测试

发布于 2025-01-23 07:16:34 字数 1073 浏览 4 评论 0原文

在我们的项目中,我们有很多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 of useJUnitPlatform().
  • Changing the name of the test from JUnit 3 style test to testSomeLibraryMethodReturnsTrue.
  • Removing the @RunWith(AllTests.class) annotation and the suite() 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文