android - 在单独的项目中创建单元测试是 Android 的正确方法吗?

标签 android unit-testing junit

关于如何开始在 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/

相关文章:

spring - 如何创建 Gradle 任务以在 Spring 中仅运行特定的测试

java - getResourceAsStream 在 jUnit 上工作,但不在 Tomcat 上工作

java - 用于 REST + DAO 对象的 Mockito

java - 使用 Activity 作为对话框

android按钮启动 Activity 错误

python - 如何在 Python 中对局部变量进行单元测试

javascript - 使用 Jasmine 和 Karma 使用失败方法对 jQuery getJSON 函数错误进行单元测试

java - JUnit - 在 @BeforeAll 之前执行的方法和在 @AfterAll 之后执行的方法

Android 应用停止使用最新版本的 PhoneGap Build

java - 如何从服务调用的线程更改 Activity 的进度条进度?