TestNG跳过测试无法实例化的测试

时间:2014-11-06 15:15:58

标签: java maven testng

据我所知,当@Before方法出错时,TestNG会有configFailurePolicy标志继续测试。但有没有办法在一个测试出错时继续运行剩余的测试...在Test类实例化期间说一些异常?

是否可以将此报告为错误并使构建失败?

以下是我的测试课程。

public class MyTest {

 public MyTest() {
  throw new RuntimeException(); 
 }

 @Test
 public void testMethod1() {
   ...
 }

 @Test
 public void testMethod2() {
   ...
 }
}

public class MyTest2 {

 @Test
 public void testMethod1() {
   ...
 }

 @Test
 public void testMethod2() {
   ...
 }
}

现在,我正在通过Maven运行这些测试:

mvn clean test

输出:

Running Test Suite
....
Cause by: org.testng.TestNGException
Cannot instantiate class ...MyTest1
....
Cause by: java.lang.RuntimeException:
....

Results:
Tests run: 0, Failed: 0, Errors: 0, Skipped: 0
....

--------------
Build Success
--------------
Total time: ..

Maven pom:

<?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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>...</groupId>
        <artifactId>...</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>war</packaging>
        <name>XYZ</name>

        <dependencies>
          ....
          <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.1.1</version>
          </dependency>
        </dependencies>

        <build>
          <plugins>
            ....
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.9</version>
            </plugin>
           </plugins>
         </build>

         <reporting>
           <outputDirectory>${basedir}/target/site</outputDirectory>
           <plugins>
             <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-surefire-report-plugin</artifactId>
               <version>2.4.1</version>
             </plugin>
            </plugins>
         </reporting>
        </project>

0 个答案:

没有答案