在 Android 中重用 junit 测试
我有一个库,我想在 Android 上重用其 Junit 3 测试。问题是这些测试依赖于存在的文件(测试数据)。
对于 Android,这意味着某些设置代码需要调用 getContext().getAssets() 并将资源文件转储到 SD 卡,测试将在其中获取它们。
问题是:我应该把这个设置代码放在哪里?我无法将其放入测试的设置中,因为它继承自 TestCase(无论如何,这意味着它不再可移植)。我不能将它放在 AllTests 套件中,因为它继承自 TestSuite。由于它是一个 Junit 项目,所以没有我可以挂钩其 onCreate 的活动。
I have a library whose Junit 3 tests I'd like to reuse on Android. The problem is that these tests depend on files being present (test data).
For Android this means that some setup code needs to call getContext().getAssets()
and dump the asset files to the sdcard, where they will be picked up by the tests.
The problem is: where do I put this setup code? I can't put it in the test's setup, as it inherits from TestCase (and anyway that would mean its no longer portable). I can't put it in the AllTests suite, as that inherits from TestSuite. Since its a Junit project there's no Activity whose onCreate I can hook.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是制作一个自定义 InstrumentationTestRunner 。重写 onCreate,其中可以存储自定义设置逻辑。例如:
The solution is to make a custom InstrumentationTestRunner. Override onCreate, where custom setup logic can be stored. For example: