Junit5 从 @AfterEach 中的测试中获取结果

发布于 2025-01-11 18:43:53 字数 582 浏览 0 评论 0 原文

我试图在 @AfterTest 中获取测试运行结果的一些上下文。我希望至少了解它是否通过,并且最好还了解抛出的异常(如果有)。

但是,我尝试的每个参数似乎都无法解析,并且我找不到任何有关可用内容的文档。

代码:

public class TestClass {
    
    @AfterEach
    public void afterEach(
        TestInfo testInfo, //works, but no report on state of test
        // none of these work
        TestExecutionSummary summary,
        TestExecutionResult result,
        TestFailure fail,
        Optional<Throwable> execOp,
        Throwable exception
    ) {
        // ...
    }
}

我该怎么做才能获取此上下文?

I am trying to get some context of the result of the test run in the @AfterTest. I would like to have, at bare minimum, knowledge if it passed or not and ideally also the thrown exception if there is one.

However, every parameter I try seems to not be resolvable and I can't find any documentation on what should be available.

Code:

public class TestClass {
    
    @AfterEach
    public void afterEach(
        TestInfo testInfo, //works, but no report on state of test
        // none of these work
        TestExecutionSummary summary,
        TestExecutionResult result,
        TestFailure fail,
        Optional<Throwable> execOp,
        Throwable exception
    ) {
        // ...
    }
}

What can I do to get this context?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傾旎 2025-01-18 18:43:53

经过尝试,我得到了以下对我有用的方法:

  1. 首先定义 TestBase,其中包含两个抽象方法,failedAfter 和 successedAfter,分别在成功或失败时调用。
public abstract class TestBase {
    protected abstract void failedAfter();

    protected abstract void succeededAfter();

    @RegisterExtension
    AfterTestExecutionCallback afterTestExecutionCallback = new AfterTestExecutionCallback() {
        @Override
        public void afterTestExecution(ExtensionContext context) throws Exception {
            Optional<Throwable> exception = context.getExecutionException();
            if (exception.isPresent()) { // has exception
                failedAfter();
            } else {                     // no exception
                succeededAfter();
            }
        }
    };
}
  1. 定义Test类,编写测试,并实现这两个抽象方法来清理资源或报告错误。
public class Test extends TestBase {
    @Override
    protected void failedAfter() {
        // fail
    }

    @Override
    protected void succeededAfter() {
        // success
    }

    @Test
    public void setAndGet() {
        // do some test
    }
}

After trying, I got the following which worked for me:

  1. First define TestBase, which includes two abstract methods, failedAfter and succeededAfter, which will be called on success or failure respectively.
public abstract class TestBase {
    protected abstract void failedAfter();

    protected abstract void succeededAfter();

    @RegisterExtension
    AfterTestExecutionCallback afterTestExecutionCallback = new AfterTestExecutionCallback() {
        @Override
        public void afterTestExecution(ExtensionContext context) throws Exception {
            Optional<Throwable> exception = context.getExecutionException();
            if (exception.isPresent()) { // has exception
                failedAfter();
            } else {                     // no exception
                succeededAfter();
            }
        }
    };
}
  1. Define the Test class, write tests, and implement these two abstract methods to clean up resources or report errors.
public class Test extends TestBase {
    @Override
    protected void failedAfter() {
        // fail
    }

    @Override
    protected void succeededAfter() {
        // success
    }

    @Test
    public void setAndGet() {
        // do some test
    }
}
我是男神闪亮亮 2025-01-18 18:43:53

不确定这是否是您想要的,但您可以使用 TestExecutionListenerTestWatcher (您还可以通过其他方式查看文档)。

可以在此处找到 TestWatcher 的示例:junit5 中的 TestWatcher 以及更详细的说明:https://www.baeldung.com/junit-testwatcher

以下代码示例部分取自 此处

public class TestResultLoggerExtension implements TestWatcher, AfterAllCallback {

   ...  

    @Override
    public void testSuccessful(ExtensionContext context) {
        System.out.println("Test Successful for test {}: ", context.getDisplayName();
    }

    @Override
    public void testFailed(ExtensionContext context, Throwable cause) {
       System.out.println("Test Failed for test {}, with cause {}", context.getDisplayName(), cause.getMessage());
    }
}

您的测试类将是这样的:

@ExtendWith(TestResultLoggerExtension.class)
public class TestClass {

您可以根据您的需要调整逻辑。

更多参考:

Not sure if this is what you want, but you can use either a TestExecutionListener or a TestWatcher (there are also other ways that you can check in documentation).

An example of TestWatcher can be found here: TestWatcher in junit5 and a more detailed explanation here: https://www.baeldung.com/junit-testwatcher.

The following code example was partially taken from here.

public class TestResultLoggerExtension implements TestWatcher, AfterAllCallback {

   ...  

    @Override
    public void testSuccessful(ExtensionContext context) {
        System.out.println("Test Successful for test {}: ", context.getDisplayName();
    }

    @Override
    public void testFailed(ExtensionContext context, Throwable cause) {
       System.out.println("Test Failed for test {}, with cause {}", context.getDisplayName(), cause.getMessage());
    }
}

You test class would be something like this:

@ExtendWith(TestResultLoggerExtension.class)
public class TestClass {

You can adapt the logic to your needs.

More References:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文