【问题标题】:Want to execute multiple TestNG test from a class for every row of data in my .CSV file想要为我的 .CSV 文件中的每一行数据从一个类执行多个 TestNG 测试
【发布时间】:2018-04-06 18:43:51
【问题描述】:

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

但这就是我要找的,我希望我的测试按以下顺序运行: 测试1 测试2 测试3 测试1 测试2 测试3 测试1 测试2 测试3

以下是我的示例代码(将我的 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

提前感谢您的帮助!

【问题讨论】:

  • 在 testng.xml 中的套件或测试标签上使用 group-by-instances="true" ....testng.org/doc/…
  • 感谢@Grasshopper - group-by-instances="true" 为我工作。
  • @Grasshopper - 也许您可以将其添加为答案,以便可以接受并关闭问题?
  • @KrishnanMahadevan 接受了您的建议并添加为答案。谢谢
  • @Sandip 您能否继续接受@Grasshopper 的回答,因为他的建议可以帮助您解决问题?

标签: java selenium testng


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多