如何执行包含在侧面测试罐中的一套 TestNG 集成测试?

How do I execute a suite of TestNG integration tests contained in a side test-jar?

我正在尝试在命令行上使用 TestNG,以便针对本地开发服务器执行一套集成测试,其中 test-jar-with-dependencies.jar 包含要执行的集成测试。

但是,src/integration-test/ 中的 none 个集成测试似乎正在执行。

$ java -classpath ".;testng-6.8.8.jar;jcommander-1.27.jar;coolthing.diagnostic-5.0-SNAPSHOT-test-jar-with-dependencies.jar" org.testng.TestNG testng.xml

=========================================
diagnostic-suite
Total tests run: 0, Failures: 0, Skips: 0
=========================================

由于 maven-jar-plugin 似乎没有打包测试依赖项,我选择 assemble 使用 maven-assembly-plugin 的测试 jar,其中程序集描述符定义如下:

<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>test-jar-with-dependencies</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <useProjectAttachments>true</useProjectAttachments>
            <unpack>true</unpack>
            <scope>test</scope>
        </dependencySet>
    </dependencySets>
</assembly>

为了将程序集作为 Maven 安装阶段的一部分执行,我附加了程序集描述符,如下所示:

<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>
    ...
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <descriptor>src/integration-test/resources/test-jar-with-dependencies.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

这部分似乎工作得很好,除了我无法执行测试 JAR 中包含的集成测试。 TestNG套件XML文件定义如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="diagnostic-suite" parallel="classes" thread-count="4">
    <test name="endpoints">
        <groups>
            <dependencies>
                 <group depends-on="ping" name="diagnostic"></group>
            </dependencies>
            <run>
                <include name="ping" />
                <include name="diagnostic" />
            </run>
        </groups>

        <classes>
            <class name="our.company.ping.CoolThingPingIT" />
            <class name="our.company.status.CoolThingIndexIT" />
            <class name="our.company.status.CoolThingConfigurationIT" />
        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->

你能想到我可能错过了一步吗?预期结果是将执行 TestNG 套件中定义的测试,但是执行 none。

尝试使用 testjar 和 xmlpath 选项..

java -cp MyProject-jar-with-dependencies.jar;MyProject.jar;MyProject-tests.jar org.testng.TestNG-testjar MyProject-tests.jar -xmlpathinjar suites/GroupBased_Tests.xml

我记录了类似的需求here

关于打包测试罐,这是我的:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <id>attach-tests</id>
            <phase>package</phase>
            <goals>
                <!-- Always build a test-jar (if test classes present) -->
                <goal>test-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

根据来自 jar 的 运行 测试,check out this question and what I have posted here too

我已经标记了与您相关的部分(依赖项部分,然后是 dependenciesToScan 部分):

<?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> <!-- <<< dependencies, incl. the "tests" dependency -->
    <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> <!-- <<< this is what makes you find the tests within the jar -->
            <dependency>${test.library.groupId}:${test.library.artifactId}</dependency>
          </dependenciesToScan>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>