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

时间:2022-09-10 05:09:17

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

有关如何开始在Android中进行测试的说明似乎不一致:

http://developer.android.com/guide/topics/testing/testing_android.html

http://developer.android.com/guide/topics/testing/testing_android.html

It says:

它说:

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.

测试项目是一个目录或Eclipse项目,您可以在其中为测试包创建源代码,清单文件和其他文件。 Android SDK包含带有ADT的Eclipse工具以及为您创建和更新测试项目的命令行。这些工具创建用于源代码和资源的目录以及测试包的清单文件。

However, then it says:

然而,它说:

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.

您可以在文件系统中的任何位置创建测试项目,但最好的方法是添加测试项目,使其根目录test /与主应用程序项目的src /目录处于同一级别。这有助于您找到与应用程序关联的测试。

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?

然后它显示了一个项目结构,其中所有测试都在您的主项目下,而不是单独的项目。但是,如果您使用ADT插件,它将创建一个单独的项目。哪个是正确的,或者是最佳实践 - 单独的单元测试项目,还是同一个项目?

3 个解决方案

#1


5  

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.

这允许您在创建断言字段时轻松引用您可能需要的任何类。

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

我这样做是有一个名为com..Data.Tests的包,并使用以下来定义我的测试:

public class AddressValidationTest extends
    ActivityInstrumentationTestCase2<InitialActivity>

This allows you to keep everything within one project.

这允许您将所有内容保留在一个项目中。

Hope this helps!

希望这可以帮助!

EDIT: 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.

编辑:您的混淆可能来自项目与包与类之间的Java术语。我认为在这种情况下,当他们说“项目”时,他们指的是一个能够进行单元测试的独立类。

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

编辑2:您不应该需要多个清单。在清单中包含以下任何测试活动:

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

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

#2


7  

Is creating unit tests in a separate project the correct approach for Android?

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

My opinion, Yes it is the best approach
If using same project there will be some extra codes in your manifest, you need to add extra class in your src.
While if you create a separate project it will not effect our main project code And by functionality ways both will work in the same way.
So why going for a confusion :)

我的意见,是的,这是最好的方法如果使用相同的项目,你的清单中会有一些额外的代码,你需要在你的src中添加额外的类。如果你创建一个单独的项目,它将不会影响我们的主项目代码。通过功能方式,两者将以相同的方式工作。那么为什么要混淆:)

EDIT: for using same project you need to add the following in you manifest file

编辑:要使用相同的项目,您需要在清单文件中添加以下内容

<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>

#3


1  

In my opinion, creating separate project for test cases is good go. Especially if your project need earlier version of JRE. For Android projects this may not apply, but for embedded Java projects where it needs older JREs.

在我看来,为测试用例创建单独的项目是很好的。特别是如果您的项目需要早期版本的JRE。对于Android项目,这可能不适用,但适用于需要较旧JRE的嵌入式Java项目。

As most testing frameworks needs at least JDK 1.5 or above, creating separate projects for the test classes is the only solution.

由于大多数测试框架至少需要JDK 1.5或更高版本,因此为测试类创建单独的项目是唯一的解决方案。

#1


5  

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.

这允许您在创建断言字段时轻松引用您可能需要的任何类。

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

我这样做是有一个名为com..Data.Tests的包,并使用以下来定义我的测试:

public class AddressValidationTest extends
    ActivityInstrumentationTestCase2<InitialActivity>

This allows you to keep everything within one project.

这允许您将所有内容保留在一个项目中。

Hope this helps!

希望这可以帮助!

EDIT: 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.

编辑:您的混淆可能来自项目与包与类之间的Java术语。我认为在这种情况下,当他们说“项目”时,他们指的是一个能够进行单元测试的独立类。

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

编辑2:您不应该需要多个清单。在清单中包含以下任何测试活动:

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

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

#2


7  

Is creating unit tests in a separate project the correct approach for Android?

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

My opinion, Yes it is the best approach
If using same project there will be some extra codes in your manifest, you need to add extra class in your src.
While if you create a separate project it will not effect our main project code And by functionality ways both will work in the same way.
So why going for a confusion :)

我的意见,是的,这是最好的方法如果使用相同的项目,你的清单中会有一些额外的代码,你需要在你的src中添加额外的类。如果你创建一个单独的项目,它将不会影响我们的主项目代码。通过功能方式,两者将以相同的方式工作。那么为什么要混淆:)

EDIT: for using same project you need to add the following in you manifest file

编辑:要使用相同的项目,您需要在清单文件中添加以下内容

<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>

#3


1  

In my opinion, creating separate project for test cases is good go. Especially if your project need earlier version of JRE. For Android projects this may not apply, but for embedded Java projects where it needs older JREs.

在我看来,为测试用例创建单独的项目是很好的。特别是如果您的项目需要早期版本的JRE。对于Android项目,这可能不适用,但适用于需要较旧JRE的嵌入式Java项目。

As most testing frameworks needs at least JDK 1.5 or above, creating separate projects for the test classes is the only solution.

由于大多数测试框架至少需要JDK 1.5或更高版本,因此为测试类创建单独的项目是唯一的解决方案。