0

我正在当前项目中研究范围报告框架。在我的测试类中,我使用 Data-provider 类来导入测试数据。特定测试包含 10 个验证测试迭代数据提供者键。当涉及范围报告时,我将方法名称写入报告但我必须区分每个数据提供者测试,并使用与每个验证测试匹配的名称。这是我的脚本:1。范围报告配置:

   @BeforeSuite
   public void setUp()
   {
       htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") +"/test-output/extentReport.html");
       extent = new ExtentReports();
       extent.attachReporter(htmlReporter);

       extent.setSystemInfo("OS", "Windows");
       extent.setSystemInfo("Host Name", "1016086");
       extent.setSystemInfo("Environment", "QA");
       extent.setSystemInfo("User Name", "BABURAJ V D");

       htmlReporter.config().setChartVisibilityOnOpen(true);
       htmlReporter.config().setDocumentTitle("Extent report");
       htmlReporter.config().setReportName("Final Report");
   }


      @BeforeClass
      public synchronized void beforeClass() {
          test = extent.createTest(getClass().getSimpleName());
          parentTest.set(test);

      }

      @BeforeMethod
      public synchronized void beforeMethod(Method method) {

          child = parentTest.get().createNode(method.getName());
          childTest.set(child);

      }
      @AfterMethod
        public synchronized void afterMethod(ITestResult result) {
            if (result.getStatus() == ITestResult.FAILURE)
                childTest.get().fail(result.getThrowable());
            else if (result.getStatus() == ITestResult.SKIP)
                childTest.get().skip(result.getThrowable());
            else
                childTest.get().pass("Test passed");

   }

   @AfterSuite

   public void tearDown(){

       extent.flush();
   }

扩展报告

@Test(priority = 2, dataProvider = GE_XR240_DEVICE_DataProvider.DEVICE_ADD_VALIDATIONS, dataProviderClass = GE_XR240_DEVICE_DataProvider.class)

public void OPTIMA_XR240_FAILED_MESSAGE_VALIDATION(Map<String, String> deviceTestData) throws InterruptedException { }

这些是在特定测试 OPTIMA_XR240_FAILED_MESSAGE_VALIDATION 中发生的验证或应该在 UI 中显示的错误消息:

Add Device Failed - System name should be filled
Add Device Failed - System Id should be filled
Add Device Failed - Manufacturer should be assigned
Add Device Failed - System model should be assigned
Add Device Failed - Current IP Address / Host Name should be filled

所以我制作的报告应该包含一条与其执行的验证相关的信息,而不仅仅是方法名称。

4

3 回答 3

0

它正在使用方法名称创建节点,因为您定义了名为“OPTIMA_XR240_FAILED_MESSAGE_VALIDATION”的方法

如果您想获得自定义名称,您可以像这样传递它:

 child = parentTest.get().createNode("Test node");
 childTest.set(child);

每次如果您想给出一些数字标识,如“测试节点 1”、“测试节点 2”等。您可以通过 Count 增量或循环来获得它,因此它的定义如下:

child = parentTest.get().createNode("Test node" +i);
childTest.set(child);

您可以在方法完成之前增加 i 值。

于 2018-08-28T09:39:26.010 回答
0

这是来自 extent-testng-plugin 的预发布片段,它将作为版本 4 的一部分发布,但将其发布在这里,因为这是您将数据提供者添加到您的方法中的方式:

public class ExtentITestListener
    implements ITestListener {

    private static final ExtentReports EXTENT = Extent.getInstance();

    private static ThreadLocal<ExtentTest> methodTest = new ThreadLocal<ExtentTest>();
    private static ThreadLocal<ExtentTest> dataProviderTest = new ThreadLocal<>();

    @Override
    public synchronized void onStart(ITestContext context) { }

    @Override
    public synchronized void onFinish(ITestContext context) {
        EXTENT.flush();
    }

    @Override
    public synchronized void onTestStart(ITestResult result) {      
        String methodName = result.getMethod().getMethodName();
        if (result.getParameters().length>0) {
            if (methodTest.get() != null && methodTest.get().getModel().getName().equals(methodName)) { } 
            else {
                createTest(result);
            }
            String paramName = Arrays.asList(result.getParameters()).toString();
            ExtentTest paramTest = methodTest.get().createNode(paramName);
            dataProviderTest.set(paramTest);
        } else {
            createTest(result);
        }
    }

    private void createTest(ITestResult result) {
        String methodName = result.getMethod().getMethodName();
        ExtentTest test = EXTENT.createTest(methodName);
        methodTest.set(test);

        String[] groups = result.getMethod().getGroups();
        if (groups.length > 0) {
            Arrays.asList(groups)
                .forEach(x -> methodTest.get().assignCategory(x));
        }
    }

    @Override
    public synchronized void onTestSuccess(ITestResult result) {
        getTest(result).pass("Test passed");
    }

    private ExtentTest getTest(ITestResult result) {
        ExtentTest t = result.getParameters() != null && result.getParameters().length>0
                ? dataProviderTest.get()
                : methodTest.get();
        return t;
    }

    @Override
    public synchronized void onTestFailure(ITestResult result) {
        getTest(result).fail(result.getThrowable());
    }

    @Override
    public synchronized void onTestSkipped(ITestResult result) {
        getTest(result).skip(result.getThrowable());
    }

    @Override
    public synchronized void onTestFailedButWithinSuccessPercentage(ITestResult result) { }

}


public class Extent 
    implements Serializable {

    private static final long serialVersionUID = 1L;

    private static class ExtentReportsLoader {

        private static final ExtentReports INSTANCE = new ExtentReports();

        static {
        }
    }

    public static synchronized ExtentReports getInstance() {
        return ExtentReportsLoader.INSTANCE;
    }

    @SuppressWarnings("unused")
    private ExtentReports readResolve() {
        return ExtentReportsLoader.INSTANCE;
    }

}

由于这是一个 ITestListener,您可以将其直接添加到您的测试或基类中,如下所示:

@Listeners(ExtentITestListenerAdapter.class)
public class MyTests {
    @Test
    public void test() {
        ...
    }
}
于 2018-08-29T01:07:01.877 回答
0

我找到了上述问题的解决方案,对我来说很好。将以下代码放在侦听器类的方法中,它可以从您的测试方法中获取参数

StringBuilder testName = new StringBuilder(result.getMethod().getMethodName());

Object[] parameters = result.getParameters();

if (parameters.length > 0) 
{

    StringBuilder p = new StringBuilder();

    for (int i = 0; i < parameters.length; i++) 
{
    //pass the parameter location which you want to add as a testname
        if(i == 4)
        {
              p.append(Utils.toString(parameters[i]));
        }                         
    }
}   
        
test = extent.createTest(testName.toString());
于 2022-01-10T20:15:21.703 回答