使用提供程序时覆盖TestNG测试名称并省略参数

时间:2017-03-10 20:11:38

标签: java testng testng-dataprovider

拥有以下示例代码...在运行测试时(以及在报表中),我希望将测试名称设置为提供程序提供的描述字段(实际上它是任何字符串)。

...但是,即使从ITest扩展,似乎所有提供程序参数都附加到TestName,我想要的只是描述。

因此实际测试名称应为“TestName1”而不是“TestName2 [1](TestName2,2,2,4)”..这是XML报告中显示的内容,以及test.aftertest名称。

import org.testng.Assert;
import org.testng.ITest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.lang.reflect.Method;

public class TestNgProviderExample implements ITest{

    @Test(dataProvider = "summationProvider")
    public void testProvider(String description, int number1, int number2, int sum) {
        Assert.assertEquals(sum, number1 + number2);
    }

    @DataProvider(name = "summationProvider")
    public Object[][] summationData() {
        Object[][] testData = {{"TestName1",1,2,3},{"TestName2",2,2,4}};
        return testData;
    }

    private String reportedTestName = "";

    @BeforeMethod(alwaysRun = true)
    public void testData(Method method, Object[] testData) {
        reportedTestName = testData[0].toString();
    }

    @Override
    public String getTestName() {
        return reportedTestName;
    }
}

1 个答案:

答案 0 :(得分:4)

简单地运行没有参数的@Test - 带注释的方法可以解决它 我的解决方案使用类字段和构造函数@Factory方法而不是参数。

public class TestClass implements ITest {

    protected String description;
    protected int firstNumber;
    protected int secondNumber;
    protected int sum;

    @Test
    public void testProvider() {
        /**
         * Minus or plus here to make it fail or pass.
         */
        assertEquals(this.sum, this.firstNumber - this.secondNumber);
    }

    @Factory(dataProvider = "summationProvider")
    public TestClass(String description,
                     int firstNumber, int secondNumber, int sum) {
        this.description = description;
        this.firstNumber = firstNumber;
        this.secondNumber = secondNumber;
        this.sum = sum;
    }

    @DataProvider(name = "summationProvider")
    public static Object[][] summationData() {
        Object[][] testData = {{"TestName1", 1, 2, 3}, {"TestName2", 2, 2, 4}};
        return testData;
    }

    @Override
    public String getTestName() {
        return this.description;
    }
}

使用IntelliJ Idea中的格式输出: enter image description here

当然,可以使@Factory实例化另一个类,而不是TestClass

如果你有40个独立的参数参数并希望保持简短...添加一个可以作为这些参数的持有者的类,例如: ParametrizedInput。它至少可以隐藏所有这些实例变量。 您也可以将description放在第二个类中(在这种情况下,建议ThreadLocal<ParametrizedInput>使用。)
第二个类将随参数增长,但由于它是一个普通的旧Java对象,它不应该在测试中破坏任何东西。 如果您不想设置所有这些变量,另一个想法是在测试中懒惰地访问参数。 按照专家(Krishnan Mahadevan)的建议,我发现可以使用@BeforeMethod - 和@AfterMethod - 带注释的方法设置带方法的名称。

public class TestClass implements ITest {

    protected static ThreadLocal<String> description
            = new ThreadLocal<>();
    protected ParametrizedInput input;

    @BeforeMethod
    public void setUp(Method method) {
        this.description.set(this.description.get() + " " + method.getName());
    }

    @AfterMethod
    public void tearDown(Method method) {
        this.description.set(this.description.get().substring(0,
                this.description.get().length() - method.getName().length()));
    }

    @Test
    public void testProvider() {
        assertEquals(this.input.getSum(),
                this.input.getFirstNumber() / this.input.getSecondNumber());
    }

    @Test
    public void anotherTestProvider() {
        assertEquals(this.input.getSum(),
                this.input.getFirstNumber() - this.input.getSecondNumber());
    }
    @Factory(dataProvider = "summationProvider")
    public TestClass(String descriptionString, ParametrizedInput input) {
        this.description.set(descriptionString);
        this.input = input;
    }

    @DataProvider(name = "summationProvider")
    public static Object[][] summationData() {
        Object[][] testData = {{"TestName1", new ParametrizedInput(1, 2, 3)},
                {"TestName2", new ParametrizedInput(2, 2, 4)}};
        return testData;
    }

    @Override
    public String getTestName() {
        return this.description.get();
    }
}

参数持有者类:

public class ParametrizedInput {

    private int firstNumber;
    private int secondNumber;
    private int sum;

    public ParametrizedInput(int firstNumber,
                             int secondNumber, int sum) {
        this.firstNumber = firstNumber;
        this.secondNumber = secondNumber;
        this.sum = sum;
    }

    public int getFirstNumber() {
        return firstNumber;
    }

    public int getSecondNumber() {
        return secondNumber;
    }

    public int getSum() {
        return sum;
    }
}

结果:
enter image description here