在一个单独的项目创建单元测试,正确的方法为Android? [英] Is creating unit tests in a separate project the correct approach for Android?

查看:257
本文介绍了在一个单独的项目创建单元测试,正确的方法为Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何开始使用测试在Android的这种描述出现不一致的:

This description of how to get started with testing in Android appears inconsistent:

<一个href="http://developer.android.com/guide/topics/testing/testing_android.html">http://developer.android.com/guide/topics/testing/testing_android.html

它说:

一个测试项目是一个目录或者Eclipse项目中,创建   源$ C ​​$ C,清单文件和其他文件的测试包。   Android SDK中包含Eclipse工具与ADT和用于   对于您创建和更新测试项目的命令行。这些工具   创建使用目录源$ C ​​$ c和资源,   清单文件测试包。

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.

不过,再这样说的:

您可以在文件系统中的任何地方创建一个测试项目,但   最好的办法是增加测试项目,这样它的根目录   测试/是在同一水平为主体的src /目录   应用程序的项目。这可以帮助你找到相关的测试   一个应用程序。

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插件,它会创建一个单独的项目。这是正确的,或最好的做法 - ?单元测试一个单独的项目,或者同一项目

Then it shows a project structure where all of your tests are under your main project and not a separate project. Yet, if you use the ADT plugin, it will create a separate project. Which is correct, or best practice -- a separate project for unit tests, or the same project?

推荐答案

这也许可以创建一个单独的项目单元测试,但我已经做它的方式是只使用了不同的封装。

It may be possible to create unit tests in a seperate project, but the way I've done it is just using a different package.

这可以让你参考,你可能需要很容易,同时创造你的断言字段的任何类。

This allows you to reference any classes you may need easily while creating your assert fields.

我做的方式是有一个包叫做com..Data.Tests和使用以下方法来定义我的测试:

The way I do it is have a package called com..Data.Tests and use the following to define my tests:

public class AddressValidationTest extends
    ActivityInstrumentationTestCase2<InitialActivity>

这可以让你把一切都在一个项目中。

This allows you to keep everything within one project.

希望这有助于!

编辑:可能你的困惑是从项目与包与类的Java术语来了。我认为,在这种情况下,当他们说一个项目,他们指的是一个独立的类,它是能够运行单元测试。

It may be possible your confusion is coming from the Java terminology of projects vs. packages vs. classes. I think in this case, when they say a "project" they are referring to a stand-alone class that is able to run a unit test.

EDIT2: 你不应该需要一个以上的清单。
包括在清单中的任何测试活动如下:

You shouldn't need more than one manifest.
Include any test activities in the manifest with the following:

<uses-library android:name="android.test.runner" />  

<instrumentation android:label="All Tests"
    android:name="com.App.Data.Tests.MyInstrumentationTestRunner"
    android:targetPackage="com.App.UI" />

这篇关于在一个单独的项目创建单元测试,正确的方法为Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆