简体   繁体   中英

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).

My scenario is something like this ...

  1. Use dataProvider to have a 2 dim array. (I am using this to read from Excel, but avoided it for brevity of the question).

     @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(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.

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.

Note: I tried handling it using @BeforeMethod, @AfterMethod but still it goes through the unwanted methods that I want to avoid.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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