Is it possible to define Activity inside Android test project and run a test against it?

后端 未结 2 992
孤街浪徒
孤街浪徒 2021-01-07 19:18

I\'m writing tests for a component in my Android application. This component uses activities to make some reports. So I need an activity in order to test the component (ugly

相关标签:
2条回答
  • 2021-01-07 19:52

    Yes, it is possible but not recommended, as it stated in the official dev guide:

    Once you have created a test project, you populate it with a test package. This package does not require an Activity, although you can define one if you wish. Although your test package can combine Activity classes, test case classes, or ordinary classes, your main test case should extend one of the Android test case classes or JUnit classes, because these provide the best testing features.

    In order to do this, you need:

    1. Define your dummy activity in Test Project's AndroidManifest.xml.
    2. Change the instrumentation targetPackage point to itself in Test Project's AndroidManifest.xml. (the Activity class under test must under the instrumentation targetPackage).

    Suppose I have a Test Project com.example.test contain two class DummyActivity and DummyActivityTest, then if you want test DummyActivity using DummyActivityTest, you need to define your Test Project's AndroidManifest.xml like this:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.test"
        android:versionCode="1"
        android:versionName="1.0" >
    
    <uses-sdk android:minSdkVersion="10" />
    
    <!-- targetPackage point to test project itself -->
    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.example.test" />
    
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
        <activity
            android:name=".DummyActivity"
            android:label="@string/app_name" >
        </activity>
    </application>
    

    0 讨论(0)
  • 2021-01-07 20:12

    The test project and activity can coexist together, put the target package name as the test project's package name

    0 讨论(0)
提交回复
热议问题