TestNG - Dataprovider at Class level test annocation

烈酒焚心 提交于 2020-01-07 06:31:48

问题


I will try and explain my scenario here so that it's clear what I want.

I have a set of methods within a Class which performs a few tests, they are all related to each other and have to be run in a sequence, so they have DependsOnMethod in their @Test annotation. I am also using a DataProvider which returns 7 values per test (n rows & 7 columns). Now when I put this data provider at the method level, it reads all the rows and executes Method 1 with every set of data available, then moves onto next method.

I want this sequence changed, I want it to run Method 1 (data set 1), Method 2 (Data set 1), Method 1 (Data set 2), ... I know this a question many people have. The reason I am posting this question again is because I have tried all the suggested ideas.

When using Factory annotation, it makes my test to not start at all in Eclipse, giving a NullPointerException. I use an @BeforeClass annotation which is never run if I use the factory annotation. I am giving this factory annotation on Constructor of the class.

I have tried the group-by-instance flag in the testng.xml file but that fails too: it runs the tests in the same sequence.

Please can someone explain this whole thing in the correct order for me? If possible with an example, as I'm sure this can be achieved but I guess I am missing something somewhere.

Any help with this is greatly appreciated.

P.S. I am not very good with Java and I been using TestNG for the past 2-3 weeks.

Thanks, John


回答1:


The easiest way is to invoke these methods manually:

@Test(dataProvider = ...)
public void f(int n1, int n2) {
    method1(n1, n2);
    method2(n1, n2);
}

However, grouping by instances should have worked, can you email a small class to the testng-users list so I can reproduce it?

Thanks.




回答2:


With the anotation "DataSet" : JavaDoc :

org.unitils.dbunit.annotation.DataSet
@Target(value={METHOD, TYPE})
@Retention(value=RUNTIME)
@Inherited

Annotation indicating that a data set should be loaded before the test run.

If a class is annotated, a test data set will be loaded before the execution of each of the test methods in the class. A data set file name can explicitly be specified. If no such file name is specified, first a data set named 'classname'.'testmethod'.xml will be tried, if no such file exists, 'classname'.xml will be tried. If that file also doesn't exist, an exception will be thrown. Filenames that start with '/' are treated absolute. Filenames that do not start with '/', are relative to the current class.

A test method can also be annotated with DataSet in which case you specify the dataset that needs to be loaded before this test method is run. Again, a file name can explicitly be specified or if not specified, the default will be used: first 'classname'.'methodname'.xml and if that file does not exist 'classname'.xml.

Examples:

 @DataSet
  public class MyTestClass extends UnitilsJUnit3 {

      public void testMethod1(){
      }

     @DataSet("aCustomFileName.xml")
      public void testMethod2(){
      }
  }

Will load a data set file named MyTestClass.xml or MyTestClass-testMethod1.xml for testMethod1 in the same directory as the class. And for testMethod2 a data set file named aCustomFileName.xml in the same directory as the class is loaded.

  public class MyTestClass extends UnitilsJUnit3 {

      public void testMethod1(){
      }

     @DataSet
      public void testMethod2(){
      }
  }

Will not load any dataset for testMethod1 (there is no class level data set). Will load a data set file named MyTestClass.xml or MyTestClass.testMethod2.xml for testMethod2. Author:

Filip Neven

Tim Ducheyne



来源:https://stackoverflow.com/questions/8893474/testng-dataprovider-at-class-level-test-annocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!