如何使用Android依赖项和robolectric运行Junit5测试 [英] How to run a Junit5 test with Android dependencies and robolectric

查看:254
本文介绍了如何使用Android依赖项和robolectric运行Junit5测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试像这样进行单元测试:

I am trying to run a unit test like that:

    @org.junit.jupiter.api.Test
    void junit5codeCoverage() {
        final int result = new Foo().junit5();

        Assert.assertEquals(Looper.getMainLooper().getThread(), Thread.currentThread());

        assertEquals(-1, result);
    }

这是对具有Robolectric的Android依赖项(即Looper.getMainLooper())的 Junit5 测试.

That is a Junit5 test with Android dependencies (i.e Looper.getMainLooper()) with Robolectric.

我正在使用 mannodermaus的Junit5 android插件,该插件允许在Android设置中运行junit5.但这开箱即用,因为它不会加载robolectric.因此,我尝试了 alixwar的分支来解决robolectric和junit5测试的覆盖问题,但仍然不会使用Android类.

I am using the junit5 android plugin from mannodermaus that allows running junit5 within Android setups. But this does not work out of the box because it would not load robolectric. So I tried alixwar's branch that would tackle robolectric and junit5 test coverage, but still, would not use Android classes.

此外,我开始研究如何在junit5上进行自动测试,这需要了解RobolectricTestRunner的工作方式以及如何将代码移植到JUnit5平台.我对新的junit5平台了解甚少,但我发现我可以在org.junit.platform.runner.JUnitPlatform运行程序的基础上构建,以遵循测试运行程序概念,该概念是junit-platform-runner程序包的一部分.但这与原始的Junit4 SandBoxTestRunner相距太远,以至于我无法完成端口.

I furthermore started to investigate how to run a robolectric test on junit5, which would require understanding how the RobolectricTestRunner works and how to port the code to the JUnit5 platform. I have little understanding of the new junit5 platform, but I figured out that I could build on top of the org.junit.platform.runner.JUnitPlatform runner, to follow the test runner concept, which is part of the junit-platform-runner package. But this is so far away from the original Junit4 SandBoxTestRunner that I couldn't manage to complete the port.

那么实现robolectric junit5支持的最可行途径是什么,或者我缺少任何(显而易见的)概念?

So what would be the most feasible path to implement robolectric junit5 support, or is there any (obvious) concept I am missing?

推荐答案

我也在尝试这样做,但是在撰写本文时,Robolectric似乎根本不支持junit5.

I am trying to do this too, but it seems that at the time of this writing, Robolectric simply does not support junit5.

请参阅此处的讨论: https://github.com/robolectric/robolectric/issues/2996

在该讨论中描述了一些解决方法,但是现在我只继续使用junit4.

Some workarounds are described in that discussion, but for now I am just going to stick to junit4.

这篇关于如何使用Android依赖项和robolectric运行Junit5测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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