【问题标题】:Stacking properties by specifying multiple Maven profiles on the command line通过在命令行上指定多个 Maven 配置文件来堆叠属性
【发布时间】:2017-07-13 05:39:34
【问题描述】:

我终于在每个测试中使用 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 

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

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

如果没有指定配置文件。

所以两个问题:

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

【问题讨论】:

    标签: java maven junit pom.xml


    【解决方案1】:

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

    <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 命令行上指定完全限定的类别类名称,您可以使用 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.$1</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.
    

    【讨论】:

    • 谢谢!一个更简单的解决方案。我唯一的抱怨是,在使用 Build Helper 插件时,fq.testcase.name 在 IDE 中没有被识别为一个值,所以它总是显示错误的值。代码仍在编译,测试仍在运行,但我的强迫症不喜欢红色的名字。
    • 也许您可以通过将 &lt;fq.testcase...&gt; 属性添加到具有空值或任何值的 &lt;properties&gt; 部分来欺骗您的 IDE
    • 由于某种原因,当我添加空白属性时,它会阻止类别工作。换句话说,它运行所有测试而不考虑优先级。
    猜你喜欢
    • 2016-04-15
    • 2015-07-22
    • 2016-07-18
    • 2017-09-25
    • 2013-06-24
    • 2012-02-17
    • 2022-06-10
    • 2010-10-19
    • 2019-07-18
    相关资源
    最近更新 更多