所有测试类中的所有测试终止后,在junit 5中运行代码 [英] Run code in junit 5 after all tests in all test classes terminated

查看:214
本文介绍了所有测试类中的所有测试终止后,在junit 5中运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为测试证据,我想将我写到特定目录中的所有测试工件(例如,服务器调用和响应)存档.我有压缩目录的方法,但是找不到合适的执行位置.

由于在每个测试类之后都调用@AfterAll,因此测试生命周期回调似乎不是正确的位置,但是应该在所有测试类中的所有测试终止后生成存档.

阅读了 https://www.codota .com/web/assistant/code/rs/5c7c89fd2f7dae0001637b13#L33 但如果我将此代码放在mavens src/test下的树中,我看不到将仅调用此启动器,而不是所有单个测试以及...

由于代码最终将在Maven构建中运行,因此归档确实很重要,因此我认为在目标测试之后可能会运行它.但是我知道,如果测试目标中至少存在一定数量的错误,那么maven将会停止,然后以下代码将不会执行.但是在错误情况下,证据将是最有用的.

有人尝试过类似的东西吗?

感谢您分享思想,想法和经验

解决方案

通常,有几种解决方案.

  • 您可以在继承所有测试的父类上定义beforeAll/afterAll.另一个选择是尝试在接口上定义beforeAll/afterAll(我不确定这是否行得通;我不确定应该这样做).在beforeAll中,您可以初始化设置,在afterEachCallBack中,您可以收集单个测试所需的所有数据.因此,在每次测试后都将其称为.<​​/p>

  • 您可以实现一个扩展,该扩展使用 BeforeAllCallback AfterAllCallBack 那.根据您的实际需求,您可能还需要实现beforeEachCallBack/afterEachCallBack.

  • 另一种选择是使用示例项目,该示例项目显示了JUnit Jupiter的用法听众.

    For test evidence I would like to archive all tests artifacts (like eg Server calls and responses) that I wrote into a specific directory. I got the method to zip the directory, but I din't find a suitable place to execute it.

    The test life cycle callbacks don't seem to be the right place since @AfterAll is called after each test class, but the archive should be generated after all tests in all test classes terminated.

    After reading posts like How to code once before and once after all junit5 tests have executed in a module? there seems to be little hope to execute the call from within junit.

    I found an extension TestLauncher, but after reading the scarce documentation this rather seems to be used to start the tests from the outside. Especially the SummaryGeneratingListener sounds useful for extending after reading https://www.codota.com/web/assistant/code/rs/5c7c89fd2f7dae0001637b13#L33 but if I put this code in the tree under mavens src/test I don't see how only this launcher will be called and not all the single tests as well...

    Since the code will eventually run in a maven build where the archiving really matters I thought maybe there is a possibility to run it after target test. But I understood that maven stops if there at least a certain amount of errors in the test target and then the following code will not be executed. But it's the error situation where the evidence will be most useful.

    Did anybody try something similar?

    Thanks for sharing thoughts, ideas and experience

    解决方案

    In general there are several solutions for the problem.

    • You could define the beforeAll/afterAll on a parent class from which all of your tests inherit. Another option would be to try to define the beforeAll/afterAll on an interface (I'm not 100% sure if this works; I would say it should). In beforeAll you could initialize your setup and in the afterEachCallBack your can collect all the data you need for a single test. So this is called after each test.

    • You could implement an extension which uses the callbacks of JUnit which are BeforeAllCallback and AfterAllCallBack which could handle that. Based on what you exactly need you may need to implement beforeEachCallBack/afterEachCallBack as well.

    • Another option would be to use the Test Execution listener where you can use testPlanExecutionStarted and testPlanExecutionFinished which you should implemented to record and get all you need.

    I've made an example project which shows the usage of the JUnit Jupiter listener.

    这篇关于所有测试类中的所有测试终止后,在junit 5中运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆