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

Is there an equivalent class like ITestResult(using testng) that is available with just JUnit?

我目前正在使用 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!");
    }
};

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

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

@BeforeTest - @BeforeClass

@AfterTest - @AfterClass

@BeforeMethod - @Before

@AfterMethod - @After

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

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