简体   繁体   English

在Android项目中使用Java模块进行单元测试有什么意义吗?

[英]Is there any point in using a Java module for unit tests in an Android project?

When developing for Android, is there any point to having a test project as a normal Java module, so when run doesn't actually run on the emulator/device? 在为Android开发时,是否有将测试项目作为普通Java模块的任何意义,所以当run在实际上不在模拟器/设备上运行时?

I had the idea to create an Android test project/module for items which depend on Android context, and a normal Java project/module for tests which have no reliance on Android itself. 我的想法是为依赖于Android上下文的项目创建一个Android测试项目/模块,并为不依赖于Android本身的测试创建一个普通的Java项目/模块。

Is it worth doing this or am I opening myself up for potential pain by not having the test code run on the device. 这样做值得吗?还是因为我没有在设备上运行测试代码而让自己为潜在的痛苦敞开心opening。

Note I come from a .Net background so my Java experience is pretty slim. 请注意,我来自.Net背景,因此我的Java经验非常苗条。

When I develop for Android, I find this very helpful. 当我为Android开发时,我发现这很有帮助。 There are some functions that do not require hardware based events, but just data. 有些功能不需要基于硬件的事件,而仅需要数据。 If you can come up with robust test data, this can be an easy, painless way to regression test your code and be confident that you haven't introduced any bugs into existing components. 如果您可以提供可靠的测试数据,那么这将是一种轻松,轻松的方法来对代码进行回归测试,并确信您没有在现有组件中引入任何错误。

This is a valid approach. 这是一种有效的方法。 Also Roboelectric is a unit testing framework that addresses the main issue that you mentioned (slowness of tests executed on emulator). Roboelectric还是一个单元测试框架,可以解决您提到的主要问题(在仿真器上执行测试的缓慢性)。 Might be worth checking out. 可能值得检查。 There are also quite a few related answers in this question . 这个问题也有很多相关的答案。

Android Developer在Hello,TestingTesting Fundamentals中涵盖了这些主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM