2

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

4

3 回答 3

2

您也可以只使用 JUnit Assume方法来决定(在测试内部)是否应该执行或跳过测试。

然而,恕我直言,最好的选择是将设备依赖测试“声明”为“集成测试”,并让它们由Maven Failsafe Plugin执行。我认为这将是没有任何配置文件“魔术”的“内置”maven 解决方案。

于 2012-07-24T10:54:34.370 回答
1

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

于 2012-07-24T10:44:28.477 回答
0

为了扩展@Jean-Rémy 的答案,我在我的项目 POM 文件中执行了以下操作:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.12</version>
      <configuration>
        <excludes>
          <exclude>${tests.to.skip}</exclude>
        </excludes>
      </configuration>
    </plugin>
  </plugins>
</build>

<profiles>
  <profile>
    <!-- This profile will be used when running tests without a device -->
    <id>default-profile</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <tests.to.skip>**/*DeviceTest.java</tests.to.skip>
    </properties>
  </profile>

  <profile>
    <id>device-profile</id>
    <activation>
      <property>
        <name>device</name>
        <value>true</value>
      </property>
    </activation>
    <properties>
      <!-- Unsure how to match nothing -->
      <tests.to.skip>NOTHING</tests.to.skip>
    </properties>
  </profile>

这将创建两个配置文件,默认配置文件将排除设备测试,而“设备配置文件”将执行所有测试。

要执行设备配置文件,可以执行mvn -Ddevice=true test.

于 2012-07-24T13:12:51.647 回答