TestNG 不可能重复上课吗? [英] Is TestNG Not Possible To Repeat Class?

查看:30
本文介绍了TestNG 不可能重复上课吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 TestNG 作为主要框架和 apache-poi 作为数据源(使用 .xlsx 文件),我会将其用于硒目的.

I'm trying to build a data driven automation test with TestNG as main framework and apache-poi as data source (using .xlsx file), I'll to use this for selenium purposes.

这是从 excel 文件中选择的场景示例:

This is example the selected scenario from excel file:

在此处查看图片详情

它可以根据使用它的用户随机选择.模式可以是全部购买、全部消耗或混合.

It can be chosen randomly, according to the user who uses it. The pattern can be purchase all, consume all, or mix.

并且期望按所选顺序运行.

And the expectations run in the order chosen.

到目前为止,这是我创建的代码:

So far this is the code I've created:

大师班

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
===============================================

我使用的是 TestNG 7.0.0

pom.xml

即使有4个选择的数据如上例图(由输出确认>classes size : 4),结果测试只运行了2个,并且运行了最新的选择.好像是忽略同一个类.

Even though there are 4 selected data like the example image above (confirmed by output > classes size : 4), it turns out that the test only runs 2 only, and run the latest choice. It seems to indicate ignoring the same class.

注意:在这个问题中只有 2 个类,实际上还有更多,只是我简化了.

Note : In this question there are only 2 classes, actually there are more than that, just I make it simpler.

这个设计是否可以用TestNG实现(与同一个类的运行权限有关)?

Is this design achievable with TestNG (related to running permissions of the same class)?

或者有什么建议可以使用另一个java测试框架?

Or is there any suggestion to use another java test framework?

推荐答案

您可以为每个具有不同参数的类创建 test 标签.

You can create test tag for every class with different parameters.

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
===============================================

这篇关于TestNG 不可能重复上课吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆