【问题标题】:How to write a test suite with junit 5 for eclipse?如何使用 junit 5 为 Eclipse 编写测试套件?
【发布时间】:2021-05-07 09:04:10
【问题描述】:

我正在尝试通过测试套件中的 junit5 测试运行。但是我得到一个错误

import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;

import com.test.studentservice.controllers.StudentControllerTest2;
import com.test.studentservice.repo.StudentServiceRepoDataTest;
import com.test.studentservice.service.StudentServiceTest;

@RunWith(JUnitPlatform.class)
@SelectClasses({StudentControllerTest2.class, StudentServiceRepoDataTest.class, StudentServiceTest.class})
public class StudentServiceTestSuite {

}

这是我得到的错误:

谢谢

【问题讨论】:

  • JUnit5 中的一个常见错误是使用org.junit.Test 而不是org.junit.jupiter.api.Test 标记测试方法。您可以右键单击一个包并选择Run As > JUnit Test,这样您就无需编写代码来列出一个包的所有测试类。如果这没有帮助,请告诉您是否可以将类作为 JUnit 测试运行并显示一个包含测试类代码的最小示例。
  • @howlger 谢谢,右键单击包并运行 jUnit 测试工作正常。但是我需要根据要求有一个测试套件。我已经重新检查并在每次测试中使用org.junit.jupiter.api.Test。我正在关注baeldung.com/junit-5 测试套件示例

标签: eclipse tdd junit5 test-suite


【解决方案1】:

您收到此错误,因为在 Test 选项卡中的 JUnit 启动配置Run > Run Configurations...)中,您选择 Test runner 选项 JUnit 5 而不是 JUnit 4。 p>

@RunWith(JUnitPlatform.class) 用于将 JUnit 5 测试作为 JUnit 4 测试运行,以防工具仅支持 JUnit 4 但不支持 JUnit 5 (@987654321 @)。但是您不需要它,因为 Eclipse 支持 JUnit 5,并且不应该使用它,因为通过 JUnit 4 API 运行它有一些限制。

Baeldung - A Guide to JUnit 5 - 7. Test Suites 其中you mentioned in a comment you are following here 是错误的,或者至少在这一点上具有误导性。代替 JUnit 测试套件类,标记您的测试并使用它们来包含或排除测试(在 JUnit 启动配置中,在 Test 选项卡中,点击 Configure... em> 按钮)。

【讨论】:

    【解决方案2】:

    从 2021 年 10 月到 11 月 jUnit5 支持@Suite 注释:

        import org.junit.platform.suite.api.SelectClasses;
        import org.junit.platform.suite.api.Suite;
        import org.junit.platform.suite.api.SuiteDisplayName;
    
        @Suite
        @SuiteDisplayName("Build Verification Test")
        // Insert the class names in the order of execution
        @SelectClasses({
                FirstTest.class,
                SecondTest.class
        })
        public class SuiteTest {
            
        }
    

    波姆:

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.jupiter.version>5.8.0</junit.jupiter.version>
        <junit.platform.version>1.8.0</junit.platform.version>
    </properties>
        ...
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <version>${junit.platform.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite-api</artifactId>
            <version>${junit.platform.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite-commons</artifactId>
            <version>${junit.platform.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite-engine</artifactId>
            <version>${junit.platform.version}</version>
            <scope>test</scope>
        </dependency>
        ...
    

    【讨论】:

      猜你喜欢
      • 2010-10-31
      • 2022-08-20
      • 2020-02-27
      • 2018-12-26
      • 2021-01-04
      • 1970-01-01
      • 2023-03-08
      • 2021-02-03
      • 2022-11-04
      相关资源
      最近更新 更多