使用 Maven 从 @Category 运行测试

发布于 2024-12-20 02:22:10 字数 989 浏览 4 评论 0原文

我查看了其他一些问题,例如 this这个。但这些问题已经过时了,我很好奇是否存在新的解决方案。

我的设置如下:

类别界面:

public interface FastTest{}

类别套件:

@RunWith(Categories.class)
@IncludeCategory(FastTest.class)
public class FastSuite{}

示例测试:

@Category(FastTest.class)
public class FastTests{

    @Test public void assertOneFastTest(){}

    @Test public void assertTwoFastTest(){}
}

使用 maven,假设我只想运行所有 FastTest 测试。理想情况下,我会使用命令

mvn test -Dtest.category=FastTest

mvn test -Dtest.suite=FastSuite

但我无法让它工作。除了使用 ClasspathSuite 之外,还有人有任何建议吗?谢谢。

I have looked at a few other SO questions like this and this. But those questions are pretty dated and I'm curious if there exists a new solution.

Here's what my setup looks like:

Category interface:

public interface FastTest{}

Category suite:

@RunWith(Categories.class)
@IncludeCategory(FastTest.class)
public class FastSuite{}

Sample test:

@Category(FastTest.class)
public class FastTests{

    @Test public void assertOneFastTest(){}

    @Test public void assertTwoFastTest(){}
}

Using maven, let's say I want to only run all my FastTest tests. Ideally, I would use the command

mvn test -Dtest.category=FastTest

or

mvn test -Dtest.suite=FastSuite

But I have not been able to get this working. Does anyone have any suggestions aside from using ClasspathSuite? Thanks.

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

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

发布评论

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

评论(2

枕梦 2024-12-27 02:22:10

您可以使用组配置从 Surefire 插件执行此操作,但您还需要指定 junit47 提供程序。以下内容对我有用:

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.11</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>2.11</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <groups>com.xxx.foo.CategoryClass</groups>
    </configuration>
</plugin>

我认为这是最近引入的,因此可能不适用于该插件的早期版本(2.11 之前)。您需要指定提供者,否则不起作用。 groups 应该是 Category 类的完全限定名称。如果需要,您还可以指定排除组。

有关更多信息,请参阅 Maven Surefire 插件surefire:test

You can do this from the surefire plugin, using the configuration for groups, but you need to specify the junit47 provider as well. The following works for me:

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.11</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>2.11</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <groups>com.xxx.foo.CategoryClass</groups>
    </configuration>
</plugin>

I think this was introduced fairly recently, so may not work in earlier versions of the plugin, pre 2.11. You need to specify the provider, otherwise it doesn't work. The groups should be the fully qualified name of the Category class. You can also specify an excludeGroups as well if needs be.

For more information, see Maven Surefire Plugin surefire:test.

靑春怀旧 2024-12-27 02:22:10

做了更多研究,发现没有选项,所以我最终使用 ClasspathSuite。事实证明,除了你不能在类级别指定@Category,你必须注释你想要分类的每个方法之外,它没有任何不同

现在,每个类别套件看起来像这样:

@RunWith(Categories.class)
@Categories.IncludeCategory(FastTest.class)
@Suite.SuiteClasses(AllTests.class)
public class FastSuite{}

你必须修改AllTests 看起来像这样:

@RunWith(ClasspathSuite.class)
public class AllTests{}

对于方法,它看起来像这样:

public class FastTests{

    @Categories(FastTest.class)
    public void assertOneFastTest(){}

    @Categories(FastTest.class)
    public void assertTwoFastTest(){}
}

使用 maven,您可以执行以下命令:

mvn test -Dtest=FastSuite -Dt3-chrome-path=%CHROME_DRIVER_HOME -Dwebdriver.chrome.driver=%CHROME_DRIVER_HOME

Did a little more research and found no options so I ended up using ClasspathSuite. It turns out that it's not any different except that you cannot specify @Category at a class level, you have to annotate every method you want to categorize

Now, each category suite looks like this:

@RunWith(Categories.class)
@Categories.IncludeCategory(FastTest.class)
@Suite.SuiteClasses(AllTests.class)
public class FastSuite{}

You have to modify AllTests to look like this:

@RunWith(ClasspathSuite.class)
public class AllTests{}

For methods, it looks like this:

public class FastTests{

    @Categories(FastTest.class)
    public void assertOneFastTest(){}

    @Categories(FastTest.class)
    public void assertTwoFastTest(){}
}

Using maven, you can do the following command:

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