我希望将参数化测试添加到使用 Robolectric 测试框架的现有测试套件中,如下所示:
@RunWith(RobolectricTestRunner.class)
public class SomeTest {
/* ... Tons of tests */
}
其中一项测试是针对过去已修复但现在采用参数的功能。 这个答案表明,为了以参数化的方式运行一个测试,我应该添加
@RunWith(Enclosed.class)
,然后将我想要参数化的测试包装在它自己的类中。不幸的是,看来我必须选择Enclosed.class
或RobolectricTestRunner.class
,而不是两者都选。
上似乎有一个与此相关的开放问题,但问题中的答案似乎是将整个测试包装在@RunWith(Enclosed.class)
中,然后将内部测试包装在带有@RunWith(RobolectricTestRunner.class)
的类中。鉴于我只更改相当大的一组测试用例中的一个测试,因此我对将所有内容重构为一组内部类的想法感到不太满意。
有什么方法可以让 Robolectric 测试运行器参数化更大套件中的一个测试吗?
我遇到了同样的问题,并设法通过使用 Enclosure 来做到这一点(代码在 kotlin 中):
@RunWith(Enclosed::class)
internal class WhateverTest {
@RunWith(ParameterizedRobolectricTestRunner::class)
internal class WhateverTestParametrized(private val brand: Brand) {
// parametrized tests
}
@RunWith(RobolectricTestRunner::class)
internal class WhateverTestNotParametrized {
// Not parametrized tests
}
}
我知道您不希望在测试中使用内部类,但这是我发现的让 Robolectric 测试运行程序根据要求参数化更大套件中的一个测试的唯一方法。