Hello!

I am trying to execute a test if and only if a certain condition is set
(such as a profile is activated).  The test would fail if it is run on some
systems, so I am trying to disable it by default, and run it only when run
on the relevant system.

I made a sample project to simplify the problem.  Below is the relevant part
my pom:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>**/ExecuteSometimesTest.java</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <id>condition-profile</id>
      <activation>
        <property>
          <name>some-condition</name>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <executions>
              <execution>
                <id>rare-test</id>
                <goals>
                  <goal>test</goal>
                </goals>
                <phase>test</phase>
                <configuration>
                  <includes>
                    <include>**/ExecuteSometimesTest.java</include>
                  </includes>
                  <excludes />
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

When I execute "mvn test" it works as expected:
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running sample.test.ExecuteAlwaysTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.085 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO]
------------------------------------------------------------------------


However, when I execute "mvn test -Dsome-condition" the
ExecuteSometimesTest.java is not executed:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running sample.test.ExecuteAlwaysTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.04 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] [surefire:test {execution: rare-test}]
[INFO] Surefire report directory:
/home/danik/workspace/test2/execute-test-conditionally/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
There are no tests to run.

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO]
------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO]
------------------------------------------------------------------------

Am I doing something wrong or is this a known problem?

Help would be much appreciated!!

-- Dan

Reply via email to