Custom test method name in TestNG reports

后端 未结 6 1883
执念已碎
执念已碎 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

    Please find following code for set custom name of testcase in TestNG reports.

    Following features are available in this code.

    • Dynamic execution on same test-case in multiple time
    • Set custom test-case name for reports
    • Set parallel execution of multiple test-cases execution

      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";
      
          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());
          }
      }}
      

      Thanks

提交回复
热议问题