簡體   English   中英

通過在命令行上指定多個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 

但是不是添加/連接,而是覆蓋,因此只運行Medium測試。 為了增加額外的難度,因為pom.xml抱怨$ {testcase.category}只存在於配置文件中,而且沒有默認值,我不得不添加:

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

如果沒有指定配置文件。

所以有兩個問題:

  1. 如何在“組”節點中正確堆疊配置文件?
  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命令行上指定完全限定的類別類名,則可以使用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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM