3

我正在使用带有 surefire 插件的 TestNg 和 Maven 来运行我的测试。我有几个不同的组件,我希望能够使用同一个 pom 在不同的时间运行它们。目前要做到这一点,我有几个不同的 XML 文件定义了一个测试套件,并且我已经设置了 pom,所以我可以做 mvn test -Dtestfile=/path 并改用那个套件。

我想知道是否有一种方法可以将 XML 文件组合成一个文件并选择基于测试名称或其他系统?

编辑:我已经用 Smoke、Sanity、Regression 定义了我的所有测试,我希望能够为给定组件运行所有回归。如果我通过 TestNG CLI 运行,我可以提供 -testnames comp1、comp2、comp3 等。其中每个组件都在一个包含多个测试 () 的 xml 套件中定义。我想知道除了使用 exec:java 插件之外,在 maven 中是否有任何方法可以做到这一点。

4

3 回答 3

8

TestNG supports groups of tests, either by specifying groups on test classes/methods in the test cases themselves, or in the suite.xml file. By using groups, you can put all your tests in one xml file. See Groups in the TestNG user guide.

The surefire plugin allows tests to be included or excluded based on group:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.8.1</version>
        <configuration>
          <groups>${testng.groups}</groups>
        </configuration>
      </plugin>

You can put all your tests in one xml file, and then choose which ones to run by setting the group or groups to include in the ${testng.groups} property, which should be a comma-delimited list of group names.

You can define value for the ${testng.groups} property in the POM using profiles, or on the command-line -Dtestng.groups=[groups to run].

于 2011-04-19T19:12:36.650 回答
6

您可以做的是定义不同的配置文件

  <profiles>
    <profile>
      <id>t1</id>
      <build>
        <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.8.1</version>
          <configuration>
            <suiteXmlFiles>
              <suiteXmlFile>testng.xml</suiteXmlFile>
            </suiteXmlFiles>
          </configuration>
        </plugin>
      </plugins>
      </build>
     </profile>
   </profiles>

并通过 mvn -Pt1 ... 从命令行调用或在配置文件中定义一个属性并在配置中使用该属性。

于 2011-04-19T19:02:52.277 回答
2

另请注意,TestNG 允许您将多个套件合并为一个,例如,如果您想将 api 和 ui 冒烟测试合并为一个套件:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="uber-smoke-suite" verbose="1" parallel="methods" thread-count="1" configfailurepolicy="continue">
  <suite-files>
    <suite-file path="smoke_api.xml" />
    <suite-file path="smoke_ui.xml" />
  </suite-files>
</suite>

这样,您也可以创建一个超级套件,将所有测试组合成一个,但仍允许您在必要时运行单个套件,例如:

-Dtestfile=smoke
-Dtestfile=smoke_api
-Dtestfile=smoke_ui
于 2016-11-05T05:20:15.033 回答