使用 Maven 从 @Category 运行测试
我查看了其他一些问题,例如 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用组配置从 Surefire 插件执行此操作,但您还需要指定 junit47 提供程序。以下内容对我有用:
我认为这是最近引入的,因此可能不适用于该插件的早期版本(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:
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.
做了更多研究,发现没有选项,所以我最终使用 ClasspathSuite。事实证明,除了你不能在类级别指定@Category,你必须注释你想要分类的每个方法之外,它没有任何不同
现在,每个类别套件看起来像这样:
你必须修改
AllTests
看起来像这样:对于方法,它看起来像这样:
使用 maven,您可以执行以下命令:
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:
You have to modify
AllTests
to look like this:For methods, it looks like this:
Using maven, you can do the following command: