0

我正在使用 TestNG 进行硒测试。有一个名为“创建公司”的测试需要在我的笔记本电脑上运行多次。

所以我为此编写了一个名为“CreateFirm”的类,各种公司的数据都保存在一个 Excel 电子表格中。

在不同的时间,我需要创建各种公司集,我使用 Excel 电子表格中的一列来控制它们,其中包含我的计算机名称。

我使用@Factory 创建我的“CreateFirm”类,它有一个@Test 方法来创建一个公司。

在 excel 电子表格中如果我以相同的顺序将 Firm1、Firm2、Firm3、Firm4 分配给我的笔记本电脑,@Factory 会以随机顺序创建它们,例如 Firm4、Firm3、Firm1、Firm2

我的问题是如何让@Factory 按我想要的顺序创建测试实例?

我的@Factory 方法是

      @Factory
  public Object[] runCreateFirm()
  {

        //This is where I get the list of test cases assigned to my laptop
        this.test_id_list=get_test_ids_for_test_run("Create Firm (class approach).xls", "Global");      

        Object[] result = new Object[this.test_id_list.size()];


        int index=0 ;
        for (String firm_id: this.test_id_list)
        {
            //This is where I get all the test data from the Excel spreadsheet
            HashMap<String,String> test_data_row=this.get_row_from_excel("Create Firm (class approach).xls", "Global", "test_case_id", firm_id);

            System.out.println("Inside Firm Factory ,index="+index +", test case id="+ test_data_row.get("test_case_id"));

            //CreateFirm is the class which will use the data and do all the UI actions to create a Firm
            result[index]=new CreateFirm(test_data_row);
            index++;
        }
        return result;
  }

XML 是

    <?xml version="1.0" encoding="UTF-8"?>
<suite name="CreateFirm Suite">
  <test name="Create Firm Test"  order-by-instances="false">
    <classes>
      <class name="regressionTests.factory.CreateFirmFactory"/>
    </classes>
  </test>
</suite>
4

3 回答 3

2

设法为我的要求编写拦截器方法。首先,我在课堂上介绍了一个名为“优先级”的新领域。我不能使用@Priority,因为我的测试类只有一个测试方法,在这种情况下,我的所有实例都将具有相同的优先级。所以我引入了一个名为priority的新字段,它将具有类应该被实例化的顺序。我在拦截器方法中使用该字段来设置实例化的顺序

public List<IMethodInstance> intercept(List<IMethodInstance> methods,ITestContext context) 
{

    List<IMethodInstance> result = new ArrayList<IMethodInstance>();
    int array_index=0;

    for (IMethodInstance m : methods)
    {
        result.add(m);
    }
    //Now iterate through each of these test methods
    for (IMethodInstance m : methods)
    {
        try {               
            //Get the FIELD object from - Methodobj->method->class->field
            Field f = m.getMethod().getRealClass().getField("priority");
            //Get the object instance of the method object              
            array_index=f.getInt(m.getInstance());
        } 
         catch (Exception e) {
            e.printStackTrace();
        }           
        result.set(array_index-1, m);           
    }

    return result;
}
于 2012-10-22T10:18:11.253 回答
1

工厂实例化测试类,它不运行它们。如果您需要特定的测试顺序,则在依赖项(组和方法)、优先级和方法拦截器之间有很多选择。

于 2012-10-09T01:32:57.433 回答
0

参考https://github.com/cbeust/testng/issues/1410获取解决方案,并从 krahadevan 给出的示例中得到提示。谢谢一堆KR。我在 Eclipse 中使用 TestNG 插件版本 6.14.0.201802161500。

只需在测试类中覆盖 toString 方法就可以了。在此 toString 方法中返回关键参数,例如 Firm1、Firm2 等。请参考以下代码以获得清晰的理解。

测试类.java

import org.testng.annotations.Test;

public class TestClass {

    String testcaseName;

    public TestClass(String testcaseName) {
        this.testcaseName = testcaseName;
    }

    @Test
    public void sendCmd() {
        System.out.println("Testcase received "+this.testcaseName);
    }

    @Override 
    public String toString() { 
        return this.testcaseName; 
    }
}

工厂类.java

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

public class FactoryClass {

    @DataProvider
    public static String[][] createTestCmds() {
       String[][] testData = new String[6][1];
        for (int i = 0, j= 6; i < 6 ; i++,j--) {
            testData[i][0]="Firm "+i;
        }
       return testData;
    }

    @Factory(dataProvider = "createTestCmds")
    public Object[] Factory(String testcaseName) {
        return new Object[] {new TestClass(testcaseName)};
    }
}

测试.java

import java.util.Collections;

import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class Test {
    public static void main(String[] args) {
        TestNG testng = new TestNG();
        XmlSuite xmlSuite = new XmlSuite();
        xmlSuite.setGroupByInstances(true);
        xmlSuite.setName("Sample_Test_Suite");
        XmlTest xmlTest = new XmlTest(xmlSuite);
        xmlTest.setName("Sample_Test");
        xmlTest.setClasses(Collections.singletonList(new XmlClass(FactoryClass.class)));
        testng.setXmlSuites(Collections.singletonList(xmlSuite));
        testng.setVerbose(2);
        System.err.println("Printing the suite xml file that would be used.");
        System.err.println(xmlSuite.toXml());
        testng.run();
    }
}

结果

Testcase received Firm 0
Testcase received Firm 1
Testcase received Firm 2
Testcase received Firm 3
Testcase received Firm 4
Testcase received Firm 5
PASSED: sendCmd on Firm 0
PASSED: sendCmd on Firm 1
PASSED: sendCmd on Firm 2
PASSED: sendCmd on Firm 3
PASSED: sendCmd on Firm 4
PASSED: sendCmd on Firm 5

===============================================
    Sample_Test
    Tests run: 6, Failures: 0, Skips: 0
===============================================


===============================================
Sample_Test_Suite
Total tests run: 6, Failures: 0, Skips: 0
===============================================

我面临的这个问题。我得到了如上所述的解决方案。

于 2019-04-18T07:49:36.373 回答