简体   繁体   English

如何将 JUnit5 标签与 Maven

[英]How to AND JUnit5 tags with Maven

Is there a way to run only the tests with each of the given tags?有没有办法只运行每个给定标签的测试? For example, can we run only the tests with tag1 AND tag2 ?例如,我们可以只运行带有tag1 AND tag2的测试吗?

We are using Maven 3.6.2 with version 2.22.2 of Surefire and JUnit 5.5.2 to run tests against many application API endpoints.我们正在使用 Maven 3.6.2 和 Surefire 2.22.2 版本和 JUnit 5.5.2 对许多应用程序 ZDB974238714CA8DE634A7CE1D083A 端点运行测试。 Each test has at least 3 tags specifying test type, application and method type.每个测试至少有 3 个标签指定测试类型、应用程序和方法类型。 Some have more.有些有更多。

We are currently running our tests from the command line to give the tester control over each run:我们目前正在从命令行运行我们的测试,以便让测试人员控制每次运行:

mvn test -Dgroups=app1,fast

The problem we run into is that the above will run all the tests tagged with app1 OR fast .我们遇到的问题是上面将运行所有标记为app1 OR fast的测试。 The result is many dozens of tests being run.结果是运行了几十个测试。 The tester's goal is to run only the "fast" tests for "app1".测试人员的目标是只为“app1”运行“快速”测试。

While it is possible to use excludedGroups , this does not help the tester until after the run.虽然可以使用excludedGroups ,但这在运行之前对测试人员没有帮助。 Additionally, we are adding tests and tags every day so what worked today may not work tomorrow.此外,我们每天都在添加测试和标签,所以今天有效的东西明天可能就无效了。

One of our goals is enabling the specification of tags on the command line and not require the tester to edit the POM to run a different combination of tests.我们的目标之一是在命令行上启用标签规范,并且不需要测试人员编辑 POM 来运行不同的测试组合。 We would like to specify our test sets from the command line and not have to touch the POM between runs.我们想从命令行指定我们的测试集,而不必在运行之间接触 POM。 Due to the growing list of test combinations, our POM would become too large to manage efficiently.由于测试组合的列表不断增加,我们的 POM 将变得太大而无法有效管理。

Is ANDing JUnit5 tags together even possible with Surefire?是否可以使用 Surefire 将 JUnit5 标记组合在一起?

In Maven Surefire/Maven Failsafe you can define groups as you already mentioned but you can define it for JUnit Jupiter (aka JUnit 5) as like this:在 Maven Surefire/Maven Failsafe 中,您可以定义您已经提到的,但您可以为 JUnit Jupiter(又名 JUnit)定义它,如下所示:

mvn test -Dgroups="app1&fast"

or或者

mvn test -Dgroups="app1|fast"

for more details take a look into the documentation.有关更多详细信息,请查看文档。

BTW: I recommend to upgrade to the most recent versions of maven-surefire-plugin or maven-failsafe-plugin .顺便说一句:我建议升级到最新版本的maven-surefire-pluginmaven-failsafe-plugin

They way I've done it was setup interfaces.他们这样做的方式是设置界面。 If there are sub groups I just make the interface extend the parent.如果有子组,我只是让接口扩展父组。

For your case it seems like you have app1 tag and some of those tests tagged fast.对于您的情况,您似乎有 app1 标记,并且其中一些测试标记得很快。 Create interface for app1 and another for fast that extends app1.为 app1 创建接口,为快速扩展 app1 创建另一个接口。 In your tests you can add @Category(app1Fast.class)在您的测试中,您可以添加 @Category(app1Fast.class)

When you call mvn test -Dgroups=tests.groups.app1Fast it will run just the fast tags.当您调用 mvn test -Dgroups=tests.groups.app1Fast 时,它将仅运行快速标签。 If you run mvn clean test -Dgroups=tests.groups.app1 it will run everything in app1.如果您运行 mvn clean test -Dgroups=tests.groups.app1 它将运行 app1 中的所有内容。

Also, you can tell I created a package called groups to add my interfaces in.此外,您可以告诉我创建了一个名为 groups 的 package 来添加我的接口。

Another also, the interface will be empty -另一个也是,界面将为空-

public interface app1 {}

public interface app1Fast extends app1{}

Sounds like you'll have multiple apps -- app1, app2, etc.听起来您将拥有多个应用程序——app1、app2 等。

public interface app2{}
public interface app2Fast extends app2{}

Probably other ways, but this is simple enough.可能还有其他方法,但这很简单。

call app1 to test everything in app1, or app1Fast for only the fast tests for app1.调用 app1 来测试 app1 中的所有内容,或者调用 app1Fast 来仅对 app1 进行快速测试。 Same for app2. app2 也一样。

Hope that helps.希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM