【问题标题】:How can I override the test method name that appears on the TestNG report?如何覆盖出现在 TestNG 报告上的测试方法名称?
【发布时间】:2019-06-28 04:14:49
【问题描述】:

如何覆盖出现在 TestNG 报告中的测试名称?我想覆盖出现在中间列的名称(当前显示为方法名称)。 这可能吗?

我试着这样做,但没有成功。

public class EchApiTest1 extends TestBase {
  ...
@BeforeTest
public void setUp() {
    restClient = new RestClientPost();
    this.setTestName( "ech: XXXXXX" );
}

还有,基类:

import org.testng.ITest;    
public class TestBase implements ITest {        
    String testName = "";    
    @Override
    public String getTestName() {
        return this.testName;
    }           
    public void setTestName( String name ) {
        this.testName = name;
    }    
}

注意:当我在 Jenkins TestNG 插件报告中查看报告详细信息时,上述代码确实有效,该报告在 Reporter 日志输出的开头将覆盖的测试名称显示为名为“Instance Name:”的字符串.在这种情况下,为什么“setTestName()”方法会更改报告中标记为“Instance Name”的字符串?

我找到的一个答案有这样的建议,但我不知道如何将 ITestResult arg 传递给 AfterMethod 方法:

@AfterMethod
public void setResultTestName( ITestResult result ) {
  try {
    BaseTestMethod bm = (BaseTestMethod)result.getMethod();
    Field f = bm.getClass().getSuperclass().getDeclaredField("m_methodName");
    f.setAccessible(true);
    f.set( bm, bm.getMethodName() + "." + your_customized_name );        
  } catch ( Exception ex ) {
  Reporter.log( "ex" + ex.getMessage() );
 }

想法?

【问题讨论】:

  • 你查看过来自 testng 用户组的 this 条目吗?
  • 还有 this 在 Stackoverflow 中?

标签: unit-testing testng


【解决方案1】:

请在 TestNG 报告中找到以下代码来设置测试用例的自定义名称。

此代码提供以下功能。

  • 在同一测试用例上多次动态执行
  • 为报告设置自定义测试用例名称
  • 设置多个测试用例执行的并行执行
    import java.lang.reflect.Field;
    import org.testng.ITest;
    import org.testng.ITestResult;
    import org.testng.Reporter;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Factory;
    import org.testng.annotations.Test;
    import org.testng.internal.BaseTestMethod;
    import com.test.data.ServiceProcessData;

    public class ServiceTest implements ITest {

    protected ServiceProcessData serviceProcessData;
    protected String testCaseName = "";

    @Test
    public void executeServiceTest() {
        System.out.println(this.serviceProcessData.toString());
    }

    @Factory(dataProvider = "processDataList")
    public RiskServiceTest(ServiceProcessData serviceProcessData) {
        this.serviceProcessData = serviceProcessData;
    }

    @DataProvider(name = "processDataList", parallel = true)
    public static Object[] getProcessDataList() {

        Object[] serviceProcessDataList = new Object[0];
        //Set data in serviceProcessDataList
        return serviceProcessDataList;
    }

    @Override
    public String getTestName() {

        this.testCaseName = "User custom testcase name";
        // this.testCaseName = this.serviceProcessData.getTestCaseCustomName();
        return this.testCaseName;
    }

    @AfterMethod(alwaysRun = true)
    public void setResultTestName(ITestResult result) {
        try {
            BaseTestMethod baseTestMethod = (BaseTestMethod) result.getMethod();
            Field f = baseTestMethod.getClass().getSuperclass().getDeclaredField("m_methodName");
            f.setAccessible(true);
            f.set(baseTestMethod, this.testCaseName);
        } catch (Exception e) {
            ErrorMessageHelper.getInstance().setErrorMessage(e);
            Reporter.log("Exception : " + e.getMessage());
        }
    }}

谢谢

【讨论】:

    【解决方案2】:

    我找到了一个“解决方法”,但我希望得到更好的答案。我希望能够在 HTML 报告中显示这个“测试名称”或“实例名称”值(不仅仅是在 Reporter.log 输出中),我开始认为这是不可能的:

    @Test(dataProvider = "restdata2")
    public void testGetNameFromResponse( TestArguments testArgs ) {
        this.setTestName( "ech: " + testArgs.getTestName() );
        Reporter.log( getTestName() );  // this magic shows test name on report
        ....
    

    使用此解决方法,用户现在可以通过查看 Reporter.log 输出来识别它是哪个测试,但我仍然希望该名称更显眼。

    我怀疑答案在于编写一个以某种方式覆盖 ITestResult.getTestNameMethod() 方法的 TestListenerAdapter?这就是我要寻找的圣杯。

    【讨论】:

      【解决方案3】:

      ‘result’对象会自动传入setResultTestName(ITestResult result)方法 当您在测试类中定义了组时,请确保将 alwaysRun=true 设置为如下所示,否则将不会执行“AfterMethod”。 @AfterMethod (alwaysRun=true)

      【讨论】:

        猜你喜欢
        • 2013-02-19
        • 1970-01-01
        • 1970-01-01
        • 2019-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-25
        • 2012-11-28
        相关资源
        最近更新 更多