【问题标题】:Using Test-ng priority when using data providers使用数据提供者时使用 Test-ng 优先级
【发布时间】:2018-11-10 07:39:15
【问题描述】:

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

测试:

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

因此该过程具有以下内容:

  1. 打开一个网页(从数据中)
  2. 检查页面上是否存在元素
  3. 输入数值

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

如果Data 中有两个值,它将执行两次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");
}

【问题讨论】:

    标签: java selenium testng testng-dataprovider


    【解决方案1】:

    在这种情况下,您可以使用工厂类。

    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>   
    

    【讨论】:

      【解决方案2】:

      根据您的测试,它运行良好,因为测试是以这种方式设计的。根据您的方案,每个步骤都是一个步骤,您也可以设置优先级。所以它首先对所有数据执行,然后对所有数据执行第二步。它看起来像 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");
      }
      

      【讨论】:

        猜你喜欢
        • 2012-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-09
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        相关资源
        最近更新 更多