【问题标题】:Not able to run a category in Junit4 tests无法在 Junit4 测试中运行类别
【发布时间】:2020-09-23 21:09:54
【问题描述】:

有一个用类别注释的测试方法:

public interface BVT {} //is placed in package net.test.categories, file name BVT.java

public class TestClass{
    @Test
    @Category(BVT.class)
    public void someTest(){
        System.out.println("smoke");
    } }

我使用 Junit 4.12 和 surefire 3.0.0-M3

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M3</version>
    <configuration>
        <forkCount>${threadCount}</forkCount>
        <reuseForks>false</reuseForks>
        <skip>false</skip>
        <groups>${testGroup}</groups>
    </configuration>
</plugin>

如果我尝试从 BVT 类别运行测试

 mvn clean test -DtestGroup="net.test.categories.BVT"

我明白了

[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

不知道为什么没有运行/跳过测试... 在 pom 配置文件中使用 created 并没有帮助 - 它只是运行所有测试。 添加此运行器依赖项也无济于事:

<dependencies>
    <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-junit47</artifactId>
        <version>3.0.0-M3</version>
    </dependency>
</dependencies>

创建套件会运行所需的测试,但我不想列出此套件类中的所有必需类,只想从命令行运行某个组

【问题讨论】:

  • 移除 surefire-junit47 依赖。让surefire-maven-plugin。自己决定。
  • @khmarbaise,我尝试添加然后删除此依赖项。没用

标签: maven junit maven-surefire-plugin


【解决方案1】:

来自documentation

您可以通过使用groups 参数来使用JUnit 类别。

使用mvn -D... 设置系统属性,而不是插件参数。

所以你需要这样的东西:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M3</version>
    <configuration>
        ...
        <groups>${myGroup}</groups>
    </configuration>
</plugin>

然后使用mvn ... -DmyGroup="net.test.categories.BVT" 运行它

【讨论】:

  • 没用。我已经尝试在 pom.xml 中直接添加 net.test.categories.BVT 并且它不起作用
  • @Roman 在这种情况下,我会说正在发生其他事情。你能删除所有类别的东西然后运行你的测试吗?
  • 我可以运行没有类别的测试。我还可以按套件中的类别运行测试,但我不喜欢这个解决方案。
【解决方案2】:

没有找到原因,但是在surefire配置中添加include部分解决了这个问题。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M3</version>
    <configuration>
        <forkCount>${threadCount}</forkCount>
        <reuseForks>false</reuseForks>
        <skip>false</skip>
        <groups>${testGroup}</groups>
        <includes>
            <include>*</include>
        </includes>
    </configuration>
</plugin>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-01-20
  • 1970-01-01
  • 1970-01-01
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多