Junit 5测试套件

发布于 2025-01-26 09:23:28 字数 1447 浏览 4 评论 0原文

我试图将一个项目从Junit 4迁移到Junit 5.8.2,而Junit-Platform-Suite-API 1.8.2。我们曾经在测试套件中组织测试课程。但是,如果我使用@SeclaTe andation @SelectClasses,则测试跑者根本找不到测试方法。当直接运行特定的测试类时,一切都很好。这发生在Eclipse和Gradle构建中。

import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({
    TestA.class
})
public class ImportantTestSuite {
}
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;

public class TestA {

    @Test
    public void reallyImportantTest() {
        assertEquals(2, 1 + 1)
    }

}

build.gradle看起来像这样

plugins {
  id 'application'
}

dependencies {
  testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
  testImplementation 'org.junit.platform:junit-platform-suite-api:1.8.2'
  testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
}

test {
    useJUnitPlatform()
    minHeapSize = '1024m'
    maxHeapSize = '1024m'
    include '**/*ImportantTestSuite*'
    ignoreFailures = true
    testLogging {
      exceptionFormat 'full'
      events 'passed', 'skipped', 'failed'
    }
  }

的想法如何在课堂上组织套房?

编辑 答案。据我所知,我正在使用@suite和@SelectClasses而不是跑步者使用该Apporach。

I'm trying to migrate a Project from JUnit 4 to JUnit 5.8.2 with junit-platform-suite-api 1.8.2. We used to organize our test classes in test suites. But if I use the @Suite annotation with @SelectClasses the test runner finds no test methods at all. When running a specific test class directly everything is fine. This happens in eclipse and gradle builds.

import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({
    TestA.class
})
public class ImportantTestSuite {
}
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;

public class TestA {

    @Test
    public void reallyImportantTest() {
        assertEquals(2, 1 + 1)
    }

}

build.gradle looks like this

plugins {
  id 'application'
}

dependencies {
  testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
  testImplementation 'org.junit.platform:junit-platform-suite-api:1.8.2'
  testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
}

test {
    useJUnitPlatform()
    minHeapSize = '1024m'
    maxHeapSize = '1024m'
    include '**/*ImportantTestSuite*'
    ignoreFailures = true
    testLogging {
      exceptionFormat 'full'
      events 'passed', 'skipped', 'failed'
    }
  }

Any idea how to organize the suites on class basis?

Edit:

Are test suites considered deprecated in JUnit5?

I already read this answers. As far as I see I'm using that apporach with @Suite and @SelectClasses and not the runner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

总攻大人 2025-02-02 09:23:28

junit-platform-suite-engine缺少...

请参阅: https://junit.org/junit5/junit5/docs/current/current/user-guide/index.html#junit-platform-suite-suite-suite-engine-eengine

dependencies {
  testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
  testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
  testImplementation 'org.junit.platform:junit-platform-suite-api:1.8.2'
  **testRuntimeOnly 'org.junit.platform:junit-platform-suite-engine:1.8.2'**
  
}

The junit-platform-suite-engine was missing...

See: https://junit.org/junit5/docs/current/user-guide/index.html#junit-platform-suite-engine

dependencies {
  testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
  testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
  testImplementation 'org.junit.platform:junit-platform-suite-api:1.8.2'
  **testRuntimeOnly 'org.junit.platform:junit-platform-suite-engine:1.8.2'**
  
}
国际总奸 2025-02-02 09:23:28

Eclipse 4.25(Eclipse 2022-09同时释放的一部分)应改善对Junit 5的支持:

测试套件向导Junit 5支持

新的Junit Test Suite Wizard 现在已经增强了现在允许使用@suite @suite注释来创建Junit 5测试套件。

要打开新的Junit Test Suite向导当前软件包,请转到new>其他Java> junit> Junit Test Suite

“测试套件向导

结果是:

“

Eclipse 4.25 (part of the Eclipse 2022-09 simultaneous release) should improve the support for JUnit 5:

test suite wizard JUnit 5 support

The New JUnit Test Suite Wizard has been enhanced to now allow the creation of a JUnit 5 test suite using the @Suite annotation.

To open the New JUnit Test Suite Wizard for the current package, go to New > Other > Java > JUnit > JUnit Test Suite.

Test Suite Wizard -- https://www.eclipse.org/eclipse/news/4.25/images/test-suite-wizard-junit5.png

The result of this is:

JUnit 5 Test Suite

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文