想要从类中为我的.CSV文件中的每一行数据执行多个TestNG测试

时间:2018-04-06 18:43:52

标签: java selenium testng

我正在尝试从我的csv文件中的每行测试数据的类中运行多个测试(@Test)。我正在使用testNG facory和dataprovider。例如,假设我的类中有test1,test2和Test3测试方法,我的CSV文件中有3组(行)测试数据。现在当我运行它时,运行secquence如下:     TEST1     TEST1     TEST1     TEST2     TEST2     TEST2     TEST3     TEST3     TEST3

但这是我正在寻找的,我希望我的测试按以下顺序运行:     TEST1     TEST2     TEST3     TEST1     TEST2     TEST3     TEST1     TEST2     TEST3

下面是我的示例代码(用自己的数据提供者替换我的csv数据和测试数据)

import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

public class TestWithDataProviderAndFactory {
     private String firstName;
        private String lastName;

        @Factory(dataProvider = "dp")
        public TestWithDataProviderAndFactory(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        @DataProvider(name = "dp")
        public static Object[][] getData() {
            return new Object[][]{
                    {"Rajeev", "Kumar"},
                    {"Sanjeev", "Kumar"},
                    {"Jony", "Dip"}
            };
        }

        @Test
        public void test1() {
            System.out.println("This is test 1");
            System.out.println("First Name: "+ firstName);
        }

        @Test
        public void test2() {
            System.out.println("This is test 2");
            System.out.println("Last Name: "+ lastName);
        }
}

输出:(这就是我得到的)

This is test 1
First Name: Rajeev
This is test 1
First Name: Jony
This is test 1
First Name: Sanjeev
This is test 2
Last Name: Kumar
This is test 2
Last Name: Dip
This is test 2
Last Name: Kumar

但是,我想执行如下测试:

This is test 1
First Name: Rajeev
This is test 2
Last Name: Kumar
This is test 1
First Name: Sanjeev
This is test 2
Last Name: Kumar
This is test 1
First Name: Jony
This is test 2
Last Name: Dip

先谢谢你的帮助!!

1 个答案:

答案 0 :(得分:0)

在testng.xml中的套件或测试标签上使用group-by-instances="true"。有关official documentation中的更多详细信息,请参阅此链接。