在JUnit 5中,如何在所有测试之前运行代码 [英] In JUnit 5, how to run code before all tests
问题描述
@BeforeAll
批注标记了要在 class 中的所有测试之前运行的方法.
The @BeforeAll
annotation marks a method to run before all tests in a class.
http://junit.org/junit5/docs /current/user-guide/#writing-tests-annotations
但是,有没有一种方法可以在所有类的 all 测试之前运行某些代码?
But is there a way to run some code before all tests, in all classes?
我想确保测试使用一组特定的数据库连接,并且必须在运行任何测试之前进行一次全局的一次性连接设置.
I want to ensure that tests use a certain set of database connections, and the global one-time setup of these connections must occur before running any tests.
推荐答案
此答案已过时,请查看Philipp Gayret的答案.
This answer is outdated, please have a look at Philipp Gayret's answer.
当前不支持此功能,但是有一个对JUnit的拉取请求 5关于此主题:引入对之前/之后回调的支持 整个测试一次 运行.
Currently this is not supported, but there is a pull request for JUnit 5 regarding this topic: Introduce support for before/after callbacks once per entire test run.
@Rob N如果您阅读此书,请接受其他答案.然后,我可以删除答案.
@Rob N If you read this please accept the other answer. Then I can delete my answer.
这篇关于在JUnit 5中,如何在所有测试之前运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!