Search code examples
mavensurefire

Enable certain Maven tests by passing a command line switch


I have a single module project that has some unit tests that require an external hardware device. I don't want these tests to execute unless I indicate that the device is available.

I feel like this is solvable using Maven properties and the SureFire exclusion/inclusion configuration, but I can't quite see how to do it. A similar question shows how to disable/enable all the tests in a project based on a Maven property, but doesn't quite answer my issue.

In summary, I wish to identify a pattern (e.g. **/*ResourceTest.java) that describes the tests I don't want to run, unless I pass a Maven property to enable them.

E.g.

mvn clean install (runs the standard tests, but skips device-related tests)

mvn -Drun.device.tests=true clean install (runs all the tests)

Thanks in advance.

(Edited to remove the misleading usage of the word "resource" > replaced with "hardware device").


Solution

  • The link you provided gave the good answer.

    The right way

    Use a mix of Profile Management and Surefire Configuration inclusion / exlcusion is the right way.

    You should ask yourself WHY you want to activate some tests dependings on a resource. The resource should always been in your classpath.

    If not, you probably just want to activate some test manually, for some tricky reasons. In that case consider this is a bad use of Maven (how would you automate that on a distant server for instance ?)

    What you asked

    If you really really want to do that, because you have some good reasons that we are not aware of, simply use this :

    This example will trigger the profile when the generated file target/generated-sources/axistools/wsdl2java/org/apache/maven is missing.

    Example from Maven official doc : http://maven.apache.org/guides/introduction/introduction-to-profiles.html

    <profiles>
      <profile>
        <activation>
          <file>
            <missing>target/generated-sources/axistools/wsdl2java/org/apache/maven</missing>
          </file>
        </activation>
        ...
      </profile>
    </profiles>
    

    As of Maven 2.0.9, the tags and could be interpolated. Supported variables are system properties like ${user.home} and environment variables like ${env.HOME}. Please note that properties and values defined in the POM itself are not available for interpolation here, e.g. the above example activator cannot use ${project.build.directory} but needs to hard-code the path target.

    You could find more information here : http://www.sonatype.com/books/mvnref-book/reference/profiles-sect-activation.html

    Hope that will help. Don't hesitate to challenge my point of view with you own reasons (even legacy code ;) ) or experience