TestNG:如何在没有@DataProvider的情况下使用多个数据集运行测试(@Test)10次?

时间:2019-01-11 16:16:13

标签: testng

这个问题上周在一次采访中被问到了。

1 个答案:

答案 0 :(得分:0)

有多种方法可以完成此任务。

  • 方法#1 -使用工厂方法来生成一个仅包含一个@Test方法的测试类的多个实例。

以下示例显示了它的外观:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

public class RunManyTimes {
  private int data;

  public RunManyTimes(int data) {
    this.data = data;
  }

  @Test
  public void testMethod() {
    System.err.println("Running with data " + data);
  }

  @Factory
  public static Object[] getData() {
    List<Object> data = new ArrayList<>();
    IntStream.rangeClosed(1, 10).forEach(value -> data.add(new RunManyTimes(value)));
    return data.toArray(new Object[0]);
  }
}

这是输出

Running with data 3
Running with data 9
Running with data 4
Running with data 7
Running with data 2
Running with data 5
Running with data 1
Running with data 8
Running with data 6
Running with data 10

===============================================
Default Suite
Total tests run: 10, Passes: 10, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0
  • 方法2 -您添加一个套件级别的参数,该参数以逗号分隔并表示将用于执行测试的值的范围。让它们在org.testng.IAlterSuiteListener的实现中进行解析,并创建多个<test>标签,每个标签包含相同的类,但与方法参数的值不同。

这里是一个示例:

测试类如下:

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class DynamicallyConstructedParameter {

  @Test
  @Parameters("data")
  public void testMethod(int data) {
    System.err.println("Parameter passed was " + data);
  }
}

收听者如下所示:

import java.util.ArrayList;
import java.util.List;
import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class ParameterInjectingListener implements IAlterSuiteListener {

  @Override
  public void alter(List<XmlSuite> suites) {
    XmlSuite xmlSuite = suites.get(0);
    List<Integer> datum = new ArrayList<>();
    String[] parameters = xmlSuite.getParameter("parameters").split(",");
    for (String parameter : parameters) {
      datum.add(Integer.parseInt(parameter.trim()));
    }

    datum.forEach(
        value -> {
          XmlTest xmlTest = new XmlTest(xmlSuite);
          xmlTest.setName("dynamic_test_" + value);
          XmlClass xmlClass = new XmlClass(DynamicallyConstructedParameter.class);
          xmlClass.getLocalParameters().put("data", Integer.toString(value));
          xmlTest.getXmlClasses().add(xmlClass);
        });
  }
}

Suite xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="54150271_suite" parallel="false" verbose="2" configfailurepolicy="continue">
  <listeners>
    <listener
      class-name="com.rationaleemotions.stackoverflow.qn54150271.ParameterInjectingListener"/>
  </listeners>
  <parameter name="parameters" value="1, 2, 3, 4, 5, 6, 7, 8, 9, 10"/>
</suite>

以下是输出:

...
... TestNG 7.0.0-beta3 by Cédric Beust (cedric@beust.com)
...
Parameter passed was 1
PASSED: testMethod(1)

===============================================
    dynamic_test_1
    Tests run: 1, Failures: 0, Skips: 0
===============================================

Parameter passed was 2
PASSED: testMethod(2)

===============================================
    dynamic_test_2
    Tests run: 1, Failures: 0, Skips: 0
===============================================
PASSED: testMethod(3)

===============================================
    dynamic_test_3
    Tests run: 1, Failures: 0, Skips: 0
===============================================

Parameter passed was 3
Parameter passed was 4
Parameter passed was 5
PASSED: testMethod(4)

===============================================
    dynamic_test_4
    Tests run: 1, Failures: 0, Skips: 0
===============================================
PASSED: testMethod(5)

===============================================
    dynamic_test_5
    Tests run: 1, Failures: 0, Skips: 0
===============================================
Parameter passed was 6
PASSED: testMethod(6)

===============================================
    dynamic_test_6
    Tests run: 1, Failures: 0, Skips: 0
===============================================
Parameter passed was 7
PASSED: testMethod(7)

===============================================
    dynamic_test_7
    Tests run: 1, Failures: 0, Skips: 0
===============================================
Parameter passed was 8
Parameter passed was 9
PASSED: testMethod(8)

===============================================
    dynamic_test_8
    Tests run: 1, Failures: 0, Skips: 0
===============================================
PASSED: testMethod(9)

===============================================
    dynamic_test_9
    Tests run: 1, Failures: 0, Skips: 0
===============================================
PASSED: testMethod(10)

===============================================
    dynamic_test_10
    Tests run: 1, Failures: 0, Skips: 0
===============================================
Parameter passed was 10

===============================================
54150271_suite
Total tests run: 10, Passes: 10, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0