在JUnit 5中参数化beforeEach/beforeAll [英] Parameterized beforeEach/beforeAll in JUnit 5

查看:245
本文介绍了在JUnit 5中参数化beforeEach/beforeAll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为一个类似数据库的小型应用程序编写一个测试.此应用程序使用查询,并且查询应返回正确的结果.这很容易在JUnit 5中实现,例如

I want to write a test for a small database-like application. This application uses queries and a query should return the correct result. This is easily implemented in JUnit 5, something like

@BeforeEach
void before() {
  database = prepareDatabase();
}

@Test
void testQuery1() {
  assertThat(database.query("query1")).isEqualTo("result1");
}

@Test
void testQuery2() {
  assertThat(database.query("query2")).isEqualTo("result2");
}

....

现在我要添加优化开关(例如查询优化器或数据库索引).无论运行优化开关如何,查询都应返回相同的结果(优化应只改变效率而不是结果).

Now I want to add optimization switches (e.g. a query optimizer or database indices). The query should return the same results regardless of the running optimization switch (optimizations should only change efficiency not results).

对于测试,这意味着我想为prepareDatabase()的其他其他实现运行相同的方法(例如,一种使用优化程序,一种使用索引,一种不使用索引).

For the test this means that I want to run the same methods for slightly other implementations of prepareDatabase() (e.g. one with optimizer, one with index, one with nothing).

我尚未为此找到合适的扩展名.我想到了为每个优化设置复制整个类,或提供来自共享父类的方法.但是,这感觉上不像JUnit 5完成此任务的方式.也许有人可以向我指出可以帮助解决此问题的功能?

I did not yet find a proper extension for this. I thought of duplicating the whole class for each optimization setting or providing the methods from a shared parent class. Yet this does not feel like the JUnit 5 way of doing this task. Maybe someone could point me to a feature that could help me with this problem?

推荐答案

缺少的JUnit5功能称为容器模板".并且是在> https://github.com/junit-team/junit5上的预定功能请求/issues/871 .

The missing JUnit5 feature is called "Container Templates" and is a scheduled feature request at https://github.com/junit-team/junit5/issues/871.

这篇关于在JUnit 5中参数化beforeEach/beforeAll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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