是否有可能定义活动的Andr​​oid测试项目内,针对它运行一个测试?测试、有可能、定义、项目

2023-09-07 15:52:36 作者:我爱的十二个少年叫EXO

我在写测试,在我的Andr​​oid应用程序的组件。此组件使用的活动,使一些报道。所以,我需要为了测试组件(丑陋的架构)的活动,我认为这会很容易地创建测试项目中的虚拟活动,比创建 ActivityInstrumentationTestCase2&LT继承测试;测试活动> ,但由于某些原因,我总是了java.lang.RuntimeException:无法为解决活动:意向{行动= android.intent.action.MAIN FLG = 0x10000000的CMP = com.xxx / .Testctivity} 例外。

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 architecture) and I thought it would be easy to create a dummy activity inside the test project and than create tests inherited from ActivityInstrumentationTestCase2<TestActivity>, but for some reason I always get java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.xxx/.Testctivity } exception.

测试活动加入到清单文件和包装似乎是正确设置。

Test activity is added into the manifest file and package seems to be set correctly.

我试图把它的两个com.xxx(包装应用)和com.xxx.test包,没有运气。但是,当我移动测试活动到目标应用程序,一切工作正常。于是,我开始琢磨有什么测试项目和我的应用程序之间的差异,是它甚至有可能测试项目里面活动。

I've tried to put it in the both com.xxx (package of the application) and com.xxx.test packages, with no luck. But when I move TestActivity into the target application everything works fine. So I started wondering what is the difference between the test project and my application and is it even possible to have activities inside the test projects.

推荐答案

是的,这是可能的,但不推荐,因为它在规定的official开发指南:

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

一旦你创建了一个测试项目,你有一个测试包填充它。这个包并不需要一个活动,但如果你愿意,你可以定义一个。虽然你的测试包可以结合活动课,测试用例类或普通班,你的主要的测试用例应该扩展了Android测试用例类或者JUnit类之一,因为这些提供最佳的测试功能。

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:

定义的测试项目的Andr​​oidManifest.xml你的虚活动。更改仪表targetPackage点本身测试项目的Andr​​oidManifest.xml。 (被测必须在Activity类根据仪表targetPackage)。

假设我有com.example.test包含两个类DummyActivity和DummyActivityTest一个测试项目,然后如果你使用DummyActivityTest要测试DummyActivity,你需要可待因测试项目的Andr​​oidManifest.xml是这样的:

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