Custom test method name in TestNG reports

后端 未结 6 1870
执念已碎
执念已碎 2020-12-05 05:10

I am working on a project where I need to invoke TestNG programatically(using data providers). Things are fine except that in the report, we are getting the name of the @Tes

6条回答
  •  执笔经年
    2020-12-05 05:35

    If you want to change the name in the HTML report, it'll be a total hack. Here's how I did it:

    public class NinjaTest {
    ...
    ...
    @AfterMethod (alwaysRun = true)
    public void afterMethod(ITestResult result, Method method) {
        try {
            //I have XML test suites organized in directories. 
            String xmlFile = result.getTestContext().getCurrentXmlTest().getSuite().getFileName();
            String suiteName = xmlFile.substring(xmlFile.lastIndexOf("\\") + 1, xmlFile.lastIndexOf(".xml"));
            String pathToFile = xmlFile.substring(0, xmlFile.lastIndexOf("\\") );
            String directory = pathToFile.substring(pathToFile.lastIndexOf("\\") + 1);
            String testMethodName = String.format("%s/%s - %s", directory, suiteName, method.getName());
    
            //Total hack to change display name in HTML report  \(^o^)/ 
            Field methodName = org.testng.internal.BaseTestMethod.class.getDeclaredField("m_methodName");
            methodName.setAccessible(true);
            methodName.set(result.getMethod(), testMethodName);
        } catch (Exception e) {
            // Eh....  ¯\_(ツ)_/¯
            e.printStackTrace();
        }
    }
    ...
    ...
    

提交回复
热议问题