Is creating unit tests in a separate project the c

2019-03-14 18:17发布

问题:

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

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.

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.

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?

回答1:

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:

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.

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


回答2:

Is creating unit tests in a separate project the correct approach for 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 :)

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:

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.

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