简体   繁体   English

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

[英]Stacking properties by specifying multiple Maven profiles on the command line

I finally got my test automation running using JUnit4 @Category for each test; 我终于使用JUnit4 @Category为每个测试运行我的测试自动化; they are marked as either PriorityHigh , PriorityMedium , or PriorityLow . 它们被标记为PriorityHighPriorityMediumPriorityLow

In my pom.xml I have each set up as a profile: 在我的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>

Which is then used in the plugin section: 然后在插件部分中使用:

<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>

My issue is when I want to test both Medium and High, I specify 我的问题是当我想要测试中等和高时,我指定

-P PriorityHigh,PriorityMedium 

But instead of adding/concatenating, they overwrite and so only Medium tests run. 但是不是添加/连接,而是覆盖,因此只运行Medium测试。 To add extra difficulty, since pom.xml complains that ${testcase.category} only exists in profiles, and no default, I had to add this: 为了增加额外的难度,因为pom.xml抱怨$ {testcase.category}只存在于配置文件中,而且没有默认值,我不得不添加:

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

in case no profile is specified. 如果没有指定配置文件。

So two questions: 所以有两个问题:

  1. How to get the profiles to stack correctly in the "groups" node? 如何在“组”节点中正确堆叠配置文件?
  2. A better way to work it if no profile is specified (all tests should run)? 如果没有指定配置文件(所有测试应该运行),更好的方法是使用它?

The easiest thing to do would be to ditch profiles and just use system properties: 最简单的方法是抛弃配置文件并只使用系统属性:

<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>

Then: 然后:

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

If you don't want to have to specify the fully qualified category class name on the Maven command line, you could use the Build Helper plugin to qualify the names for you: 如果您不想在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>

Then: 然后:

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

mvn verify
# run PriorityLow and PriorityHigh tests

# etc.

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

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