maven2 TestNG

TestNG 在maven2 的pom.xml文件里的配置:

Configuring TestNG

To get started with TestNG, include the following dependency in your project:

[...]
  <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>5.8</version>
    <scope>test</scope>
    <classifier>jdk15</classifier>
  </dependency>
[...]

Note: if you are using JDK 1.4 Javadoc annotations for your TestNG tests, replace jdk15 with jdk14 above.

This is the only step that is required to get started - you can now create tests in your test source directory (eg, src/test/java , and as long as they are named using the defaults such as *Test.java they will be run by Surefire as TestNG tests.

If you'd like to use a different naming scheme, you can change the includes parameter, as discussed in the Inclusions and Exclusions of Tests example.

Using Suite XML Files

Another alternative is to use TestNG suite XML files. This allows flexible configuration of the tests to be run. These files are created as normal, and then added to the Surefire Plugin configuration:

[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4.2</version>
    <configuration>
      <suiteXmlFiles>
        <suiteXmlFile>testng.xml</suiteXmlFile>
      </suiteXmlFiles>
    </configuration>
  </plugin>
[...]

This configuration will override the includes and excludes patterns and run all tests in the suite files.

Specifying Test Parameters

Your TestNG test can accept parameters with the @Parameters annotation. You can also pass parameters from Maven into your TestNG test, by specifying them as system properties, like this:

[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4.2</version>
    <configuration>
      <systemProperties>
        <property>
          <name>browser</name>
          <value>firefox</value>
        </property>
      </systemProperties>
    </configuration>
  </plugin>
[...]

For more information about setting system properties in Surefire tests, see System Properties .

Using Groups

TestNG allows you to group your tests. You can then execute a specific group or groups. To do this with Surefire, use the groups parameter, for example:

[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4.2</version>
    <configuration>
      <groups>functest,perftest</groups>
    </configuration>
  </plugin>
[...]

Likewise, the excludedGroups parameter can be used to run all but a certain set of groups.

Running tests in parallel

TestNG allows you to run your tests in parallel, including JUnit tests. To do this, you must set the parallel parameter, and may change the threadCount parameter if the default of 5 is not sufficient. For example:

[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4.2</version>
    <configuration>
      <parallel>methods</parallel>
      <threadCount>10</threadCount>
    </configuration>
  </plugin>
[...]

This is particularly useful for slow tests that can have high concurrency, or to quickly and roughly assess the independance and thread safety of your tests and code.

Using custom listeners and reporters

TestNG provides support for attaching custom listeners, reporters, annotation transformers and method interceptors to your tests. By default, TestNG attaches a few basic listeners to generate HTML and XML reports.

You can configure multiple custom listeners like this:

[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <properties>
        <property>
          <name>usedefaultlisteners</name>
          <value>false</value> <!-- disabling default listeners is optional -->
        </property>
        <property>
          <name>listener</name>
          <value>com.mycompany.MyResultListener,com.mycompany.MyAnnotationTransformer,com.mycompany.MyMethodInterceptor</value>
        </property>
        <property>
          <name>reporter</name>
          <value>listenReport.Reporter</value>
        </property>
      </properties>
    </configuration>
  </plugin>
[...]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值