通过在命令行上指定多个 Maven 配置文件来堆叠属性

Stacking properties by specifying multiple Maven profiles on the command line

我终于在每个测试中使用 JUnit4 @Category 实现了测试自动化 运行;它们被标记为 PriorityHighPriorityMediumPriorityLow.

在我的 pom.xml 中,我分别设置了个人资料:

<profile>
    <id>PriorityHigh</id>
    <properties>
        <testcase.category>com.categories.PriorityHigh</testcase.category>
    </properties>
</profile>
<profile>
    <id>PriorityMedium</id>
    <properties>
        <testcase.category>com.categories.PriorityMedium</testcase.category>
    </properties>
</profile>
<profile>
    <id>PriorityLow</id>
    <properties>
        <testcase.category>com.categories.PriorityLow</testcase.category>
    </properties>
</profile>

然后在插件部分使用:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${surefire.version}</version>
    <configuration>
        <groups>${testcase.category}</groups>
        <systemPropertyVariables>
            <automation.driver>${browser.name}</automation.driver>
        </systemPropertyVariables>
    </configuration>
</plugin>

我的问题是当我想同时测试中级和高级时,我指定

-P PriorityHigh,PriorityMedium 

但它们不会 adding/concatenating,而是覆盖,因此只对 Medium 进行测试 运行。为了增加额外的难度,因为 pom.xml 抱怨 ${testcase.category} 只存在于配置文件中,没有默认值,我不得不添加这个:

<testcase.category>com.categories.PriorityHigh,com.categories.PriorityMedium,com.categories.PriorityLow</testcase.category>

如果没有指定配置文件。

所以两个问题:

  1. 如何让配置文件在 "groups" 节点中正确堆叠?
  2. 如果没有指定配置文件,更好的工作方式(所有测试都应该 运行)?

最简单的做法是放弃配置文件,只使用系统属性:

<properties>
  <testcase.category>com.categories.PriorityHigh,com.categories.PriorityLow</testcase.category>
</properties>

...

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19.1</version>
  <configuration>
    <groups>${testcase.category}</groups>
    ...
  </configuration>
</plugin>

然后:

mvn verify -Dtestcase.category=com.categories.PriorityHigh
# runs PriorityHigh tests

mvn verify -Dtestcase.category=com.categories.PriorityHigh,com.categories.PriorityLow
# runs PriorityHigh and PriorityLow tests

mvn verify
# runs PriorityHigh and PriorityLow tests

如果您不想在 Maven 命令行上指定完全限定的类别 class 名称,您可以使用 Build Helper 插件为您限定名称:

<properties>
  <testcase.category>PriorityHigh,PriorityLow</testcase.category>
</properties>

...

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>3.0.0</version>
  <executions>
    <execution>
      <id>build-fq-testcase-category</id>
      <goals>
        <goal>regex-property</goal>
      </goals>
      <configuration>
        <name>fq.testcase.category</name>
        <regex>([^,]+)</regex>
        <value>${testcase.category}</value>
        <replacement>com.categories.</replacement>
      </configuration>
    </execution>
  </executions>
</plugin>

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19.1</version>
  <configuration>
    <groups>${fq.testcase.category}</groups>
  </configuration>
</plugin>

然后:

mvn verify -Dtestcase.category=PriorityHigh
# just run PriorityHigh tests

mvn verify
# run PriorityLow and PriorityHigh tests

# etc.