使用数据提供者时使用 Test-ng 优先级

Using Test-ng priority when using data providers

我正在使用带有 test-ng 的数据提供程序,我希望特定测试对数据收集对象中的每个元素执行一系列步骤。

测试:

For each element in the object, validate the form can input the values

因此流程如下:

  1. 打开一个网页(来自资料)
  2. 检查页面上是否存在元素
  3. 输入值

我尝试使用下面的内容,但是,对于对象中的每个元素,它运行第 1 步然后移动到第 2 步,而不是遵循该过程。因此,我想问是否可以使用 test-ng 进行 'test step' 方法?

如果 Data 中有 2 个值,它将执行 Open 两次,然后继续执行 CheckElementExists

@Test (priority = 1, dataProvider = "Data")
public void Open(Data data) throws InterruptedException
{ 
    System.out.println("Step 1");
    this.module.open(data);
}

@Test (priority = 2, dataProvider = "Data")
public void CheckElementExists(Data data)
{
   System.out.println("TWO");
}

根据您的测试,它工作正常,因为测试是按照这种方式设计的。根据您的场景,每个步骤都是一个步骤,您也可以设置优先级。所以它首先对所有数据执行,然后对所有数据执行第二步。它看起来像 BDD 风格。您可以尝试使用任何 BDD 框架,如 Cucumber、jbehave 等。

如果您想使用 testng 对每个数据重复所有步骤。然后将所有步骤合并到一个测试中,然后使用数据提供程序迭代数据,如下所示。

@Test (priority = 1, dataProvider = "Data")
public void OpenAndCheck(Data data) throws InterruptedException
{ 
    System.out.println("Step 1");
    this.module.open(data);
    System.out.println("TWO");
}

在这种情况下,您可以使用 Factory class。

public class TestCase {
    Data data;

    @Factory(dataProvider = "Data")
    public TestCase(Data data){
        this.data=data;
    }

    @Test(priority = 1)
    public void Open() throws InterruptedException {
        System.out.println("Step 1");
        this.module.open(data);
    }

    @Test(priority = 2)
    public void CheckElementExists(Data data) {
        System.out.println("TWO");
    }
}

您需要在您的 testng 套件 xml 文件中提及 group-by-instance = true 并使用 xml 套件

运行
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test Suite New"  group-by-instances="true" configfailurepolicy="continue" preserve-order="true">
   <test name="Test Case">
      <classes>

         <class name="com.package.TestCase"></class>

      </classes>
   </test>
</suite>