关于如何开始在 Android 中进行测试的描述似乎不一致:
http://developer.android.com/guide/topics/testing/testing_android.html
上面写着:
A test project is a directory or Eclipse project in which you create the source code, manifest file, and other files for a test package. The Android SDK contains tools for Eclipse with ADT and for the command line that create and update test projects for you. The tools create the directories you use for source code and resources and the manifest file for the test package.
但是,它说:
You can create a test project anywhere in your file system, but the best approach is to add the test project so that its root directory tests/ is at the same level as the src/ directory of the main application's project. This helps you find the tests associated with an application.
然后它会显示一个项目结构,其中您的所有测试都在您的主项目下,而不是一个单独的项目。但是,如果您使用 ADT 插件,它将创建一个单独的项目。哪个是正确的,还是最佳实践 - 用于单元测试的单独项目,还是同一个项目?
最佳答案
Is creating unit tests in a separate project the correct approach for Android?
我的意见,是的,这是最好的方法
如果使用同一个项目,你的 manifest
中会有一些额外的代码,你需要在你的 src 中添加额外的类。
如果你创建一个单独的项目,它不会影响我们的主项目代码
通过功能方式,两者的工作方式相同。
那么为什么要混淆:)
编辑:要使用相同的项目,您需要在 list 文件中添加以下内容
<manifest>
<!-- For doing JUnit test--->
<instrumentation
android:targetPackage="com.your.package.where.testclasses"
android:name="android.test.InstrumentationTestRunner" />
<application ...>
<activity..>
<!-- For doing JUnit test -->
<uses-library android:name="android.test.runner" />
</application>
</manifest>
关于android - 在单独的项目中创建单元测试是 Android 的正确方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6859807/