为什么testng不允许重复类

1tu0hz3e  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(362)

我正在尝试构建一个数据驱动的自动化测试,使用testng作为主框架,apachepoi作为数据源(使用 .xlsx 文件)。
实际上我会用它来 selenium 的目的,但我没有把 selenium 标签,因为我认为没有相关的问题。
以下是从excel文件中选择的场景示例:

请参见此处的图像细节
它可以随机选择,根据用户谁使用它。模式可以是全部购买、全部消费或混合。
期望值按选择的顺序运行。
到目前为止,这是我创建的代码:
硕士班

public class Master {
    public static void main(String args[]) throws IOException {
        List<XmlSuite> xmlSuites = new ArrayList<>();

        XmlSuite xmlSuite = new XmlSuite();
        xmlSuite.setName("My Suite");

        List<XmlTest> xmlTests = new ArrayList<>();

        XmlTest xmlTest = new XmlTest(xmlSuite);
        xmlTest.setName("My Test");

        Datatable datatable = new Datatable();
        String sourceData = "/path/myFile.xlsx"; 
        int scenarioRowCount = datatable.getRowCount(sourceData, 0);

        XmlClass xmlClass = null;
        List<XmlClass> xmlClasses = new ArrayList<>();
        for(int i=1; i<=scenarioRowCount; i++) {
            String scenario = datatable.getCellDatatable(sourceData, 0, i, 0);

            Map<String, String> classParam = new HashMap<String, String>();
            classParam.put("scenario", scenario);

            if(scenario.contains("purchase")) {
                xmlClass = new XmlClass(Purchase.class);
                xmlClass.setParameters(classParam);
            }else if (scenario.contains("consume")) {
                xmlClass = new XmlClass(Consume.class);
                xmlClass.setParameters(classParam);
            }
            xmlClasses.add(xmlClass);
        }

        System.out.println("classes size : " +xmlClasses.size());

        xmlTest.setClasses(xmlClasses);
        xmlTests.add(xmlTest);

        xmlSuite.setTests(xmlTests);
        xmlSuites.add(xmlSuite);

        TestNG testNG = new TestNG();
        testNG.setXmlSuites(xmlSuites);
        testNG.run();
    }
}

采购类

public class Purchase {
    @Test
    @Parameters({"scenario"})
    public void purchase(String scenario) {
        System.out.println(scenario);

        //selenium code perform here, purchase a package base on selected
        ....
        ....
    }
}

消费类

public class Consume {
    @Test
    @Parameters({"scenario"})
    public void consume(String scenario) {
        System.out.println(scenario);

        //selenium code perform here, consume the quota for a application base on selected
        ....
        ....
    }
}

输出

classes size : 4
purchase quota / 20GB
consume quota / instagram 

===============================================
My Suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

我正在使用testng7.0.0
pom.xml文件
尽管有4个选定的数据,如上面的示例图像(由output>classes size:4确认),但测试只运行2个,并运行最新的选择。它似乎表示忽略同一类。
注意:这个问题只有2个类,实际上不止这些,只是我把它简化了。
这个设计是否可以通过testng实现(与运行同一类的权限相关)?
或者有没有建议使用另一个java测试框架?

bvjveswy

bvjveswy1#

可以使用不同的参数为每个类创建测试标记。

public class Master {
    public static void main(String args[]) throws IOException {
        List<XmlSuite> xmlSuites = new ArrayList<>();

        XmlSuite xmlSuite = new XmlSuite();
        xmlSuite.setName("My Suite");

        List<XmlTest> xmlTests = new ArrayList<>();

   //     XmlTest xmlTest = new XmlTest(xmlSuite);
   //     xmlTest.setName("My Test");

        Datatable datatable = new Datatable();
        String sourceData = "/path/myFile.xlsx"; 
        int scenarioRowCount = datatable.getRowCount(sourceData, 0);

      //  XmlClass xmlClass = null;
     //   List<XmlClass> xmlClasses = new ArrayList<>();
        for(int i=1; i<=scenarioRowCount; i++) {
            String scenario = datatable.getCellDatatable(sourceData, 0, i, 0);

            XmlClass xmlClass = null;
            List<XmlClass> xmlClasses = new ArrayList<>();

            XmlTest xmlTest = new XmlTest(xmlSuite);
            xmlTest.setName("My Test" + i);

            Map<String, String> classParam = new HashMap<String, String>();
            classParam.put("scenario", scenario);

            if(scenario.contains("purchase")) {
                xmlClass = new XmlClass(Purchase.class);
                xmlClass.setParameters(classParam);
            }else if (scenario.contains("consume")) {
                xmlClass = new XmlClass(Consume.class);
                xmlClass.setParameters(classParam);
            }
            xmlClasses.add(xmlClass);
            xmlTest.setClasses(xmlClasses);
            xmlTests.add(xmlTest);

        }

    //    System.out.println("classes size : " +xmlClasses.size());

     //   xmlTest.setClasses(xmlClasses);
     //   xmlTests.add(xmlTest);

        xmlSuite.setTests(xmlTests);

        System.out.println(xmlSuite.toXml());
        xmlSuites.add(xmlSuite);

        TestNG testNG = new TestNG();
        testNG.setXmlSuites(xmlSuites);
        testNG.run();
    }
}

. 只是示例输出。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="My Suite">
  <test thread-count="5" name="My Test0">
    <classes>
      <class name="testngtest.TestOne">
        <parameter name="scenario" value="purchase/10"/>
      </class> <!-- testngtest.TestOne -->
    </classes>
  </test> <!-- My Test0 -->
  <test thread-count="5" name="My Test1">
    <classes>
      <class name="testngtest.TestOne">
        <parameter name="scenario" value="purchase/20"/>
      </class> <!-- testngtest.TestOne -->
    </classes>
  </test> <!-- My Test1 -->
  <test thread-count="5" name="My Test2">
    <classes>
      <class name="testngtest.TestTwo">
        <parameter name="scenario" value="consume/10"/>
      </class> <!-- testngtest.TestTwo -->
    </classes>
  </test> <!-- My Test2 -->
  <test thread-count="5" name="My Test3">
    <classes>
      <class name="testngtest.TestTwo">
        <parameter name="scenario" value="consume/40"/>
      </class> <!-- testngtest.TestTwo -->
    </classes>
  </test> <!-- My Test3 -->
</suite> <!-- My Suite -->

TestOnepurchase/10
TestOnepurchase/20
TestOneconsume/10
TestOneconsume/40

===============================================
My Suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================

相关问题