How to run TestNG suite with failsafe without unpa

2020-07-26 05:47发布

问题:

I'd like to execute integration tests using a TestNG suite file which is contained in a dependency jar.

The details of setting up the pom.xml are discussed here

Here's the pom file I currently have. The problem is with the part where I need to define a suite file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>test</groupId>
  <artifactId>test-runner</artifactId>
  <version>1.0.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>${test.library.groupId}</groupId>
      <artifactId>${test.library.artifactId}</artifactId>
      <version>${test.library.version}</version>
    </dependency>
    <dependency>
      <groupId>${test.library.groupId}</groupId>
      <artifactId>${test.library.artifactId}</artifactId>
      <version>${test.library.version}</version>
      <classifier>tests</classifier>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.18</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>${test.suite}.xml</suiteXmlFile> <!-- <<< this is the issue -->
          </suiteXmlFiles>
          <dependenciesToScan>
            <dependency>${test.library.groupId}:${test.library.artifactId}</dependency>
          </dependenciesToScan>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

The tests can be executed by providing values for the parameters in the Jenkins job:

    -Dtest.library.groupId=com.example.test
    -Dtest.library.artifactId=example
    -Dtest.library.version=2.0.1
    -Dtest.suite=smoke

This all works fine if the suite file is available locally, but I'd like to make it work with the suite file only being available in the jar (same as the test classes). No unpackaging.

So the question is: How do I define the location of the suite file (which is packaged in the dependencies)?

This is the problematic part:

<suiteXmlFile>[whatComesHere?]${test.suite}.xml</suiteXmlFile>

How can I point failsafe / surefire at a suite file that is contained in the test jar? Or is this not possible and I would have to unpack the jar just for the sake of being able to run a specific suite file?

回答1:

Using surefire plugin specify following properties in configuration section:

        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${version}</version>
        <configuration>
          <properties>
            <property>
              <name>testjar</name>
              <value>your.test.jar</value>
            </property>
            <property> <!--use if testng.xml is not in root directory-->
              <name>xmlpathinjar</name>
              <value>resources/testng.xml</value>
            </property>
          </properties>
        </configuration>