Using TestNG, is it possible to dynamically change the test name?

▼魔方 西西 提交于 2020-04-11 05:04:58

问题


Using TestNG, is it possible to dynamically change the test name with a method such as this one below?

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

回答1:


No, but your test class can implement org.testng.ITest and override getTestName() to return the name of your test.




回答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



来源:https://stackoverflow.com/questions/12147435/using-testng-is-it-possible-to-dynamically-change-the-test-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!