简体   繁体   English

如何从TestNG数据提供者跳过数据集(出错)?

[英]How to skip a data set (on error) from TestNG data provider?

I am trying to execute a data driven test using TestNG (& of course the dataprovider annotation). 我正在尝试使用TestNG(当然还有dataprovider注释)执行数据驱动的测试。

My scenario is something like this ... 我的情况是这样的...

  1. Use dataProvider to have a 2 dim array. 使用dataProvider具有2个dim数组。 (I am using this to read from Excel, but avoided it for brevity of the question). (我正在使用它从Excel中读取,但为简洁起见,避免使用它)。

     @DataProvider(name = "featureTest") public Object[][] dataSets() throws Exception { return new Object[][] { {"TC_01", "testuser_1", "Test@123", "ABC Street", "123-456-7899" }, { "TC_02", "testuser_1", "Test@123", "PQR Street", "222-456-7899" } }; } 
  2. In the @Test method, there are several methods as per the functional flow - 在@Test方法中,根据功能流程有几种方法-

     @Test(dataProvider = "featureTest") public void executeTest(String... data) throws Exception { try{ feature_1.execute(data); feature_2.execute(data); feature_3.execute(data); feature_4.execute(data); } catch(Exception e){ log.error("Error has occured"); } } 

Now my main problem is that the functional error can occur anywhere out of these 4 (n) methods that I specify in my @Test. 现在我的主要问题是,在我在@Test中指定的这4(n)个方法中的任何地方都可能发生功能错误。

In case of an exception in any of the methods, I need to "Skip" the particular dataset and proceed to the next one. 如果任何一种方法都有例外,我需要“跳过”特定的数据集并继续进行下一个。 For eg: In during execution of TC_01, an exception occured in feature_2.execute(), it should not execute the feature_3 and feature_4 methods. 例如:在执行TC_01的过程中,feature_2.execute()中发生了异常,它不应执行feature_3和feature_4方法。

Note: I tried handling it using @BeforeMethod, @AfterMethod but still it goes through the unwanted methods that I want to avoid. 注意:我尝试使用@ BeforeMethod,@ AfterMethod来处理它,但仍然会通过我要避免的不需要的方法。

Thanks in advance for your help/inputs && apologies for the long question although a relatively simple concept to explain !!! 在此先感谢您的帮助/投入和歉意,尽管这个问题很简单,但可以解释!!!

One approach I can think of is the factory approach, 我能想到的一种方法是工厂方法,

Your testclass 您的测试课

class Test{
  Data data;
  Test(Data){
  this.data=data;
}


@Test
test1(){
  feature_1.execute(data);
}

@Test
test2(dependsOnMethods ="test1"){
  feature_2.execute(data);
}

@Test(dependsOnMethods ="test2")
test3(){
  feature_3.execute(data);
}

@Test(dependsOnMethods ="test3")
test4(){
  feature_4.execute(data);
}


}

And in your factory class 而在您的工厂班级

class Factory{

@Factory(DataProvider = "myDP")
public Object[] factoryTest(Data data){
    new Test(data);
}


@DataProvider
public Object [][] myDP(){

    enter code here
}

}

Its very simple. 非常简单。 Use return! 使用退货!

Use a condition or try catch to evaluate the failure and then use a return statement; 使用条件或尝试catch评估失败,然后使用return语句;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM