java - 是否有类似 ITestResult(使用 testng)的等效类仅适用于 JUnit?

标签 java junit4 extentreports data-driven-tests selenium-extent-report

我目前正在使用 ExtentReport 和 TestNG 来获取我的测试结果。我现在只能使用 JUnit,所以我想知道如何在不使用 TestNG 的情况下将我当前的代码转换为具有相同的功能

我看过其他人使用 TestWatcher 或 RunListener 的例子,但我不确定如何根据我的需要实现它。尤其是对我正在使用的一切都是新的

@BeforeTest
 public void setup() {
     htmlReporter = new ExtentHtmlReporter(new File(fileLocation));
     htmlReporter.loadXMLConfig(new File(FileReaderManager.getInstance().getConfigReader().getReportConfigPath()));
     reports = new ExtentReports();
     reports.attachReporter(htmlReporter);
}

@AfterTest
public void cleanup() {
    reports.flush();
}

@BeforeMethod
public void register(Method method) {
    String testName = method.getName();
    testInfo = reports.createTest(testName);

}

@AfterMethod
public void captureStatus(ITestResult result) {
    if (result.getStatus() == ITestResult.SUCCESS) {
        testInfo.log(Status.PASS, "Test: " + result.getName() + " passed");
    } else if (result.getStatus() == ITestResult.FAILURE) {
        testInfo.log(Status.PASS, "Test: " + result.getName() + " failed");
        testInfo.log(Status.FAIL, "Test FAILURE: " + result.getThrowable());
    } else if (result.getStatus() == ITestResult.SKIP) {
        testInfo.log(Status.PASS, "Test: " + result.getName() + " skipped");
    }
}

`

我想在不使用 ITestResult 和其余的 testNG 注释的情况下从这些函数中重新生成相同的结果

最佳答案

您要做的是创建一个处理测试结果的规则。此规则将创建为 TestWatcher,然后您将覆盖其失败和成功的方法。 示例:

@Rule
public TestWatcher watchman = new TestWatcher() {
    @Override
    protected void failed(Throwable e, Description description) {
        // Handle logging of failing tests
        System.out.println("Fail!");
    }

    @Override
    protected void succeeded(Description description) {
        // Handle logging of succeeding tests
       System.out.println("Success!");
    }
};

描述参数包含有关失败类的信息http://junit.sourceforge.net/javadoc/org/junit/runner/Description.html

至于方法注释,以下注释是 TestNG - JUnit 4 等效项:

@BeforeTest - @BeforeClass

@AfterTest - @AfterClass

@BeforeMethod - @Before

@AfterMethod - @After

以下代码演示了如何构建测试类:

public class MyTestClass {
    @Rule
    public TestWatcher watchman = new TestWatcher() {
        @Override
        protected void failed(Throwable e, Description description) {
            // Handle logging of failing tests
            System.out.println("Fail!");
        }

        @Override
        protected void succeeded(Description description) {
            // Handle logging of succeeding tests
            System.out.println("Success!");
        }
    };

    @BeforeClass
    public static void setup() {
        //Some setup before all test methods
    }

    @AfterClass
    public static void cleanup() {
        // Some cleanup after all test methods
    }

    @Before
    public void register() {
        // Some setup before each test method
    }

    @Test
    public void succeedingTest() {
        Assert.assertTrue(true);
    }

    @Test
    public void failingTest() {
        Assert.assertTrue(false);
    }
}

关于java - 是否有类似 ITestResult(使用 testng)的等效类仅适用于 JUnit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57980469/

相关文章:

java - 对测试失败进行截图

java - 如何在 java selenium 中将屏幕截图附加到范围报告

Java 选择列表元素

java - 如何对发送到 Activity 的不同 Extras 执行不同的操作?

java - 构建器模式的线程安全实现

java - 从 TestWatcher 跳过测试

java - 测试JPA java.lang.ClassNotFoundException : play. db.jpa.JPABase

java - JUnit 测试 "Times Out"尽管执行速度很快?

Java 数学函数像这样的数学方程吗?

java - 如何在 testng extent-report 版本 2.41.2(相关代码)中添加数据提供者值