【问题标题】:TestNG skipped classes once one test class is failed一旦一个测试课程失败,TestNG 就跳过课程
【发布时间】:2014-02-06 03:39:29
【问题描述】:

我正在使用 testng 运行一个包含大约 20 个测试类的包,我的 xml 文件是这样的

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="PageTest" parallel="classes" thread-count="5"> 
    <packages>  
        <package name="testpagename"/>         
    </packages> 
</test>     
</suite>

我的测试类被定义为

public class testdemo {
    @BeforeClass    
    public void setUpClass(){       

    }
    @AfterClass
    public void afterClass(){
    }

    @Test
    public void testMethod1(){
    }

    @Test(dependsOnMethods = {"testMethod1"})
    public void testMethod2(){
    }

}

在运行测试时,由于某种原因,其中一个测试类由于意外异常而失败,我希望以下测试类继续运行,直到所有类都执行完毕。但实际上,由于失败,其余的课程都被跳过了,有人可以告诉我如何解决这个问题吗?

谢谢

【问题讨论】:

  • 所有类都不会被跳过,只有依赖的方法应该被跳过。这个类和其他类之间有关系还是那些完全独立的类?

标签: testng


【解决方案1】:

将完整代码放入try块中,并使用catch块通过assert语句使其失败。所以下一个TestNG方法不会受到上一个失败的影响。

@Test
Public void method1(){
try{
// Here will ur code which may cause exception
}
Catch(Exception e){
   Assert.fail("  ");
}
}
@Test
Public void method2{
// implement in same way as method 1

}

【讨论】:

    【解决方案2】:

    我猜如果@Before* 方法失败,以下所有类都将被跳过。尝试在套件定义中使用 configfailurepolicy="continue" 属性。

    【讨论】:

    • 嗨,是的,我认为你是对的。但是一旦我将 configfailurepolicy="continue" 添加到套件定义中,一旦 @beforeclass annonation 失败,它只会在子类中运行没有依赖关系的测试,这是预期的吗?
    • @user3204645 请改写问题。什么是没有依赖的测试?什么是儿童班?举个例子真的会有所帮助......
    【解决方案3】:

    TestNG 文档中指定我们可以通过两种方式创建依赖项:

    1.硬依赖。您所依赖的所有方法都必须已运行并成功才能运行。如果您的依赖项中至少发生一次故障,您将不会被调用并在报告中标记为 SKIP。

    2.软依赖。这是您要求的。您将始终在您依赖的方法之后运行,即使其中一些方法失败了。当您只想确保您的测试方法以特定顺序运行但它们的成功并不真正取决于其他人的成功时,这很有用。通过在 @Test 注释中添加“alwaysRun=true”来获得软依赖。

    @Test(dependsOnMethods = {"testMethod1"},alwaysRun=true)
    public void testMethod2(){
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多