junit5_JUnit5 TestSuite替代

junit5

junit5

JUnit4具有TestSuite类来聚合多个测试。 这在JUnit 5中不可用。通常,通过套件中的一堆命名测试进行的测试发现有些糟透了。 但是,如果目标不是测试发现,而是不同测试类之间的资源共享,那么创建一个父级是有意义的。

JUnit 5提供了@Nested批注,以允许子类在其父类的上下文中运行。 假设子类是非静态的,因此可以访问其父类的实例值。 如果要共享测试资源,则可能需要考虑测试套件的类级别设置,并以某种方式将其连接到子类的类级别设置中。

让我们来设计一个伪造的例子来说明这个问题:

 @Testcontainers // use docker images @Testcontainers // use docker images
 class MyTest {

    // make a DB at the start of the test in a docker container

    // takes a few minutes to boot up

    @Container

    private static final DatabaseContainer DB = createDbContainer(); 
    private static MyDao dao; 
    @BeforeAll

    static void beforeAll() {

        dao = createDaoFrom(DB);

    }

    @Test

    void daoFeatureOne() {

       assertThat(dao.find( "no data" )).isEmpty();

    }
 }

上面是一个测试,它在测试类的全局生命周期中启动数据库。 它将一个dao对象连接到它,并且可以进行多个测试以重用该dao

在理想情况下,我们可以为每个测试重置所有内容,但是数据库是启动的昂贵资源。 也许我们可以添加一些beforeEachafterEach挂钩来清理其数据,但是我们不想退回数据库。 每一次。 类似地,如果每次运行,我们的dao一些框架启动成本可能是不希望的。

上面作为我们项目中的唯一测试是可以的,但是如果还有其他需要该数据库的测试又该怎么办呢?如果真的需要AGES运行呢?

JUnit 5中没有套件

是不是很烦如果我们可以做的话:

 @JUnit5TestSuite // not real
 @Children ({MyDaoTest. class , MyOtherDaoTest. class })
 @Testcontainers
 class MyTestSuite {

    @Container

    private static final DatabaseContainer DB = createDbContainer();
 }

那将是很棒的……但是这会给我们带来一些问题:

  • 我们如何确保测试不在套件之外运行?
  • 这些测试如何访问“ DB”对象?

套房的替代选择

假设我们有一个静态方法getDb在需要时提供数据库。

现在,让我们重写原始的DaoTest来使用它,并使其抽象化,以便测试运行程序不会将其拾取:

 abstract class MyTestImpl implements DbProvider {

    private static MyDao dao; 
    @BeforeAll

    static void beforeAll() {

        // access to the database container

        // from the static method (statically imported)

        dao = createDaoFrom(getDb());

    }

    @Test

    void daoFeatureOne() {

       assertThat(dao.find( "no data" )).isEmpty();

    }
 }

现在我们可以在套件中运行部分测试,让我们定义套件。 我们还使用@Nested来插入子类:

 @Testcontainers // use docker images @Testcontainers // use docker images
 class MyTest {

    // make a DB at the start of the test in a docker container

    // takes a few minutes to boot up

    @Container

    private static final DatabaseContainer DB = createDbContainer(); 
    // provide the getDb function to access the container

    public static DatabaseContainer getDb() {

        return DB;

    }

    // test suite members are just nested classes that extend

    // the abstract class of each member of the suite

    @Nested

    class MyTest extends MyTestImpl {

    }

    // ... add more suite members with more @Nested
 }

缺点

由于类在彼此的静态位上运行,因此存在产生混乱的风险。

每个嵌套类都必须是子类的事实也有点时髦……

但这有效并且构成了有效的测试套件。

翻译自: https://www.javacodegeeks.com/2020/04/junit5-testsuite-alternative.html

junit5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值