【发布时间】:2021-08-19 16:47:14
【问题描述】:
将您的 Android Studio 升级到北极狐会导致您返回并调整您的构建,如下所述:Junit5 testSuite with SelectClasses not working in android unit test
这是否意味着我必须从使用 JUnit 4 调整我的所有单元测试?
【问题讨论】:
标签: android-studio junit robolectric junit-jupiter
将您的 Android Studio 升级到北极狐会导致您返回并调整您的构建,如下所述:Junit5 testSuite with SelectClasses not working in android unit test
这是否意味着我必须从使用 JUnit 4 调整我的所有单元测试?
【问题讨论】:
标签: android-studio junit robolectric junit-jupiter
是的。 JUnit4 运行时 (Test Runner) 不兼容。如果您不将测试切换为使用 Junit Jupiter,则在告诉测试类执行时会收到“未收到测试事件”。
另一个问题是要意识到 Roboelectric 不适用于木星。对于这些测试,您仍然需要使用 JUnit 4 @Test 并在您的依赖项中拥有 Jupiter 的老式测试运行器:How to run a Junit5 test with Android dependencies and robolectric
gradle 构建变化总结:
android {
testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
unitTests.all {
useJUnitPlatform() // <--- this is the important part
}
}
对于依赖项:
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.7.1'
//testImplementation 'junit:junit:4.+' // should get rid of this old JUnit.
testImplementation 'org.junit.vintage:junit-vintage-engine' //Let's Jupiter do the right thing when encountering a @RunWith(RobolectricTestRunner::class)
}
【讨论】: