MivaScott
MivaScott

Reputation: 1806

Stacking properties by specifying multiple Maven profiles on the command line

I finally got my test automation running using JUnit4 @Category for each test; they are marked as either PriorityHigh, PriorityMedium, or PriorityLow.

In my pom.xml I have each set up as a profile:

<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. To add extra difficulty, since pom.xml complains that ${testcase.category} only exists in profiles, and no default, I had to add this:

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

Upvotes: 4

Views: 1951

Answers (1)

ryanp
ryanp

Reputation: 5127

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:

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

Upvotes: 4

Related Questions