首页 > 解决方案 > 在 testng 的电子邮件报告中打印 @BeforeMethod 和 @AfterMethod 的 Reporter 日志

问题描述

我在 testng 的 aftermethod 中运行了一些清理代码。一切都很顺利,但日志没有打印在可发送电子邮件的报告上。但它打印在控制台上。有没有办法在可发送电子邮件的report.html中打印前方法和后方法的日志

提供示例代码

import java.io.IOException;
import java.lang.reflect.Method;

import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class AutoItTest {
     @BeforeMethod(alwaysRun = true)
        public void initialize(Method method) {
         Reporter.log("Reporter before");
            String methodName = method.getName();
            int parameterCount = method.getParameterCount();
            String className = this.getClass().getCanonicalName();
            Reporter.log("methodName: "+methodName);
            Reporter.log("parameterCount: "+parameterCount);
            Reporter.log("className: "+className);
            long threadId = Thread.currentThread().getId();
            Reporter.log("threadId before: "+threadId);
            Test test = method.getAnnotation(Test.class);
            if (test == null) {
                return;
            }
            Reporter.log(test.testName() + " is currently running");
            Reporter.log(test.description() + " was its description");
        }

        @AfterMethod(alwaysRun = true)
        public void uninitialize(Method method) {
        Reporter.log("Reporter after");
            String methodName = method.getName();
            int parameterCount = method.getParameterCount();
            String className = this.getClass().getCanonicalName();
            long threadId = Thread.currentThread().getId();
            Reporter.log("threadId after: "+threadId);
            Reporter.log("methodName: "+methodName);
            Reporter.log("parameterCount: "+parameterCount);
            Reporter.log("className: "+className);
        }

        @Test(groups = "vdi")
        public void VDITest() throws IOException, Exception {
            Reporter.log("VDITest");
            Reporter.log("Reporter VDITest");
            long threadId = Thread.currentThread().getId();
            Reporter.log("threadId VDITest: "+threadId);
        }

        @Test(groups = "vdi")
        public void VDITest123() throws IOException, Exception {
            Reporter.log("VDITest123");
            long threadId = Thread.currentThread().getId();
            Reporter.log("threadId VDITest123: "+threadId);
        }
}

可通过电子邮件发送的报告

我想以某种方式集成 beforemethod 和 aftermethod 日志

标签: seleniumselenium-webdriverjunitwebdrivertestng

解决方案


我找到了答案

@AfterMethod
public void reportTest(ITestResult result) {
  Reporter.setCurrentTestResult(result);
  Reporter.log("Message to be printed");
}

我已经添加Reporter.setCurrentTestResult(result);了@BeforeMethod 和@AfterMethod 以及打印在可发送电子邮件的报告上的连续日志。


推荐阅读