[英]In JUnit 5, how to run code before all tests
@BeforeAll
注释标记在类中的所有测试之前运行的方法。
http://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations
但是有没有办法在所有测试之前在所有类中运行一些代码?
我想确保测试使用一组特定的数据库连接,并且必须在运行任何测试之前对这些连接进行全局一次性设置。
这现在可以通过创建自定义扩展在 JUnit5 中实现,您可以从中注册一个在根测试上下文上的关闭挂钩。
你的扩展看起来像这样;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL;
public class YourExtension implements BeforeAllCallback, ExtensionContext.Store.CloseableResource {
private static boolean started = false;
@Override
public void beforeAll(ExtensionContext context) {
if (!started) {
started = true;
// Your "before all tests" startup logic goes here
// The following line registers a callback hook when the root test context is shut down
context.getRoot().getStore(GLOBAL).put("any unique name", this);
}
}
@Override
public void close() {
// Your "after all tests" logic goes here
}
}
然后,您需要至少执行一次的任何测试类都可以注释为:
@ExtendWith({YourExtension.class})
当你在多个类上使用这个扩展时,启动和关闭逻辑只会被调用一次。
@Philipp Gayret已经提供的答案在并行测试 JUnit 时存在一些问题(即junit.jupiter.execution.parallel.enabled = true
)。
因此,我将解决方案调整为:
import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
public class BeforeAllTestsExtension extends BasicTestClass
implements BeforeAllCallback, ExtensionContext.Store.CloseableResource {
private static boolean started = false;
// Gate keeper to prevent multiple Threads within the same routine
final static Lock lock = new ReentrantLock();
@Override
public void beforeAll(final ExtensionContext context) throws Exception {
// lock the access so only one Thread has access to it
lock.lock();
if (!started) {
started = true;
// Your "before all tests" startup logic goes here
// The following line registers a callback hook when the root test context is
// shut down
context.getRoot().getStore(GLOBAL).put("any unique name", this);
// do your work - which might take some time -
// or just uses more time than the simple check of a boolean
}
// free the access
lock.unlock();
}
@Override
public void close() {
// Your "after all tests" logic goes here
}
}
如下所述,JUnit5 提供了一个自动扩展注册。 为此,在src/test/resources/
添加一个名为/META-INF/services
的目录,并添加一个名为org.junit.jupiter.api.extension.Extension
的文件。 将您班级的完全分类名称添加到此文件中,例如
at.myPackage.BeforeAllTestsExtension
接下来在同一个 Junit 配置文件中启用
junit.jupiter.extensions.autodetection.enabled=true
有了这个扩展程序会自动附加到您的所有测试中。
目前这不受支持,但是有关于此主题的JUnit 5的拉取请求: 在整个测试运行中引入对回调之前/之后的支持 。
根据@Philipp 的建议,这里有一个更完整的代码片段:
import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
public abstract class BaseSetupExtension
implements BeforeAllCallback, ExtensionContext.Store.CloseableResource {
@Override
public void beforeAll(ExtensionContext context) throws Exception {
// We need to use a unique key here, across all usages of this particular extension.
String uniqueKey = this.getClass().getName();
Object value = context.getRoot().getStore(GLOBAL).get(uniqueKey);
if (value == null) {
// First test container invocation.
context.getRoot().getStore(GLOBAL).put(uniqueKey, this);
setup();
}
}
// Callback that is invoked <em>exactly once</em>
// before the start of <em>all</em> test containers.
abstract void setup();
// Callback that is invoked <em>exactly once</em>
// after the end of <em>all</em> test containers.
// Inherited from {@code CloseableResource}
public abstract void close() throws Throwable;
}
如何使用:
public class DemoSetupExtension extends BaseSetupExtension {
@Override
void setup() {}
@Override
public void close() throws Throwable {}
}
@ExtendWith(DemoSetupExtension.class)
public class TestOne {
@BeforeAll
public void beforeAllTestOne { ... }
@Test
public void testOne { ... }
}
@ExtendWith(DemoSetupExtension.class)
public class TestTwo {
@BeforeAll
public void beforeAllTestTwo { ... }
@Test
public void testTwo { ... }
}
测试执行顺序为:
DemoSetupExtension.setup (*)
TestOne.beforeAllTestOne
TestOne.testOne
TestOne.afterAllTestOne
TestTwo.beforeAllTestTwo
TestTwo.testTwo
TestTwo.afterAllTestTwo
DemoSetupExtension.close (*)
...无论您选择运行单个@Test(例如TestOne.testOne),还是整个测试类(TestOne),或多个/所有测试,这都是正确的。
您可以使用定义static
BeforeAll
的接口标记使用您的数据库的每个测试类(以便它不能被覆盖)。 例如:
interface UsesDatabase {
@BeforeAll
static void initializeDatabaseConnections() {
// initialize database connections
}
}
此方法将为每个实现类调用一次,因此您需要定义一种方法来初始化您的连接一次,然后对其他调用不做任何事情。
我不知道这样做的方法。
我会简单地确保@BeforeAll 的所有代码都调用某个单例来使该 init 工作(可能以一种懒惰的方式避免重复)。
可能不方便……我看到的唯一其他选择:我假设您的测试在特定的 JVM 作业中运行。 您可以将代理挂接到该 JVM 运行中,这样 init 就可以为您工作。
除此之外:这两个建议对我来说听起来有点像黑客。 我眼中的真正答案是:退后一步,仔细检查您的环境的依赖关系。 然后找到一种方法来准备您的环境,使您的测试出现并且“正确的事情”自动发生。 换句话说:考虑研究给你带来这个问题的架构。
以上建议为我做不工作,所以我解决了这个问题是这样的:
将这部分代码添加到您的 Base 抽象类(我的意思是您在setUpDriver()方法中初始化驱动程序的抽象类)中:
private static boolean started = false;
static{
if (!started) {
started = true;
try {
setUpDriver(); //method where you initialize your driver
} catch (MalformedURLException e) {
}
}
}
现在,如果你的测试类从抽象基类继承- > setUpDriver()方法将被执行的每个项目运行之前,首先@Test一次。
这是我对@Phillip Gayret非常好的回答的 POC 改进,紧随@Mihnea Giurgea的脚步。
我的子问题:如何访问共享的单例资源? (也许你也想知道这个……)
边栏:在我的实验中,我发现使用多个@ExtendWith(...)
似乎可以正确嵌套。 即便如此,我记得在我摸索的某个时候它不是那样工作的,所以你应该确保你的用例正常工作[原文如此]。
因为您可能很着急,所以先来甜点:这是运行“文件夹内的所有测试”的输出:
NestedSingleton::beforeAll (setting resource)
Singleton::Start-Once
Base::beforeAll
Colors::blue - resource=Something nice to share!
Colors::gold - resource=Something nice to share!
Base::afterAll
Base::beforeAll
Numbers::one - resource=Something nice to share!
Numbers::tre - resource=Something nice to share!
Numbers::two - resource=Something nice to share!
Base::afterAll
Singleton::Finish-Once
NestedSingleton::close (clearing resource)
当然,只运行一个测试类就可以得到:
NestedSingleton::beforeAll (setting resource)
Singleton::Start-Once
Base::beforeAll
Numbers::one - resource=Something nice to share!
Numbers::tre - resource=Something nice to share!
Numbers::two - resource=Something nice to share!
Base::afterAll
Singleton::Finish-Once
NestedSingleton::close (clearing resource)
和一个特定的测试,正如现在可以预期的那样:
NestedSingleton::beforeAll (setting resource)
Singleton::Start-Once
Base::beforeAll
Colors::gold - resource=Something nice to share!
Base::afterAll
Singleton::Finish-Once
NestedSingleton::close (clearing resource)
还在我这儿? 然后你可能会喜欢看到实际的代码......
======================================================
junitsingletonresource/Base.java
======================================================
package junitsingletonresource;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;
@ExtendWith({Singleton.class})
public abstract class Base extends BaseNestedSingleton
{
@BeforeAll public static void beforeAll() { System.out.println("Base::beforeAll"); }
@AfterAll public static void afterAll () { System.out.println("Base::afterAll" ); }
}
======================================================
junitsingletonresource/Colors.java
======================================================
package junitsingletonresource;
import org.junit.jupiter.api.Test;
public class Colors extends Base
{
@Test public void blue() { System.out.println("Colors::blue - resource=" + getResource()); }
@Test public void gold() { System.out.println("Colors::gold - resource=" + getResource()); }
}
======================================================
junitsingletonresource/Numbers.java
======================================================
package junitsingletonresource;
import org.junit.jupiter.api.Test;
public class Numbers extends Base
{
@Test public void one() { System.out.println("Numbers::one - resource=" + getResource()); }
@Test public void two() { System.out.println("Numbers::two - resource=" + getResource()); }
@Test public void tre() { System.out.println("Numbers::tre - resource=" + getResource()); }
}
======================================================
junitsingletonresource/BaseNestedSingleton.java
======================================================
package junitsingletonresource;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL;
/**
* My riff on Phillip Gayret's solution from: https://stackoverflow.com/a/51556718/5957643
*/
@ExtendWith({BaseNestedSingleton.NestedSingleton.class})
public abstract class BaseNestedSingleton
{
protected String getResource() { return NestedSingleton.getResource(); }
static /*pkg*/ class NestedSingleton implements BeforeAllCallback, ExtensionContext.Store.CloseableResource
{
private static boolean initialized = false;
private static String resource = "Tests should never see this value (e.g. could be null)";
private static String getResource() { return resource; }
@Override
public void beforeAll(ExtensionContext context)
{
if (!initialized) {
initialized = true;
// The following line registers a callback hook when the root test context is shut down
context.getRoot().getStore(GLOBAL).put(this.getClass().getCanonicalName(), this);
// Your "before all tests" startup logic goes here, e.g. making connections,
// loading in-memory DB, waiting for external resources to "warm up", etc.
System.out.println("NestedSingleton::beforeAll (setting resource)");
resource = "Something nice to share!";
}
}
@Override
public void close() {
if (!initialized) { throw new RuntimeException("Oops - this should never happen"); }
// Cleanup the resource if needed, e.g. flush files, gracefully end connections, bury any corpses, etc.
System.out.println("NestedSingleton::close (clearing resource)");
resource = null;
}
}
}
======================================================
junitsingletonresource/Singleton.java
======================================================
package junitsingletonresource;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL;
/**
* This is pretty much what Phillip Gayret provided, but with some printing for traceability
*/
public class Singleton implements BeforeAllCallback, ExtensionContext.Store.CloseableResource
{
private static boolean started = false;
@Override
public void beforeAll(ExtensionContext context)
{
if (!started) {
started = true;
System.out.println("Singleton::Start-Once");
context.getRoot().getStore(GLOBAL).put("any unique name", this);
}
}
@Override
public void close() { System.out.println("Singleton::Finish-Once"); }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.