5

使用 TestNG,是否可以使用如下方法动态更改测试名称?

@Test(testName = "defaultName", dataProvider="tests")
public void testLogin( int num, String reportName )
{
    System.out.println("Starting " + num + ": " + reportName);
    changeTestName("Test" + num);
}
4

2 回答 2

4

不,但是您的测试类可以实现org.testng.ITest并覆盖 getTestName() 以返回您的测试名称。

于 2012-08-27T19:50:02.970 回答
2

For anybody still facing this.
This can be done by implementing the org.testng.ITest class and overriding the getTestName() method just like as @Cedric mentions.
To make the test name dynamic you can use a locally created testName variable In addition.
Below is all you need to do

import java.lang.reflect.Method;
import org.testng.ITest;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;

public class MyTestClass implements ITest {

    @Test(dataProvider = "/* yourDataProvider */")
    public void myTestMethod() {
        //Test method body
    }

    @BeforeMethod(alwaysRun = true)
    public void setTestName(Method method, Object[] row) {
        //You have the test data received through dataProvider delivered here in row
        String name = resolveTestName(row);
        testName.set(name);
    }

    @Override
    public String getTestName() {
        return testName.get();
    }
    private ThreadLocal<String> testName = new ThreadLocal<>();
}

This way you should be able to generate the testName dynamically

于 2019-07-17T09:20:48.763 回答