简体   繁体   English

在所有测试类中的所有测试终止后运行 junit 5 中的代码

[英]Run code in junit 5 after all tests in all test classes terminated

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.我得到了 zip 目录的方法,但是我找不到合适的地方来执行它。

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.测试生命周期回调似乎不是正确的位置,因为在每个测试 class 之后调用 @AfterAll,但是应该在所有测试类中的所有测试终止后生成存档。

After reading posts like How to code once before and once after all junit5 tests have executed in a module?在阅读了如何在模块中执行所有 junit5 测试之前和之后如何编码等帖子之后? there seems to be little hope to execute the call from within junit.从 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.我找到了一个扩展 TestLauncher,但是在阅读了稀缺的文档之后,这似乎被用来从外部开始测试。 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...特别是在阅读https://www.codota.com/web/assistant/code/rs/5c7c89fd2f7dae0001637b13#L33之后,SummaryGeneratingListener 听起来对扩展很有用,但是如果我将此代码放在 mavens src/test 下的树中,我看不到如何只有这个启动器会被调用,而不是所有的单个测试......

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.由于代码最终将在 maven 构建中运行,其中归档非常重要,我认为也许有可能在目标测试后运行它。 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.但是我知道如果测试目标中至少有一定数量的错误,maven就会停止,然后下面的代码将不会被执行。 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.您可以在所有测试继承的父 class 上定义 beforeAll/afterAll。 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).另一种选择是尝试在接口上定义 beforeAll/afterAll(我不能 100% 确定这是否有效;我会说它应该)。 In beforeAll you could initialize your setup and in the afterEachCallBack your can collect all the data you need for a single test.在 beforeAll 中,您可以初始化设置,在 afterEachCallBack 中,您可以收集单个测试所需的所有数据。 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.您可以实现一个扩展,它使用 JUnit 的回调,它们是可以处理的BeforeAllCallbackAfterAllCallBack Based on what you exactly need you may need to implement beforeEachCallBack/afterEachCallBack as well.根据您的确切需要,您可能还需要实现 beforeEachCallBack/afterEachCallBack。

  • 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.另一种选择是使用Test Execution 侦听器,您可以在其中使用testPlanExecutionStartedtestPlanExecutionFinished ,您应该实施它们来记录和获取所需的一切。

I've made an example project which shows the usage of the JUnit Jupiter listener.我制作了一个示例项目,展示了 JUnit Jupiter 监听器的用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM