在androidTest文件夹中创建DummyActivity以进行测试

Sre*_*dhu 23 android unit-testing android-espresso android-instrumentation

我在androidTest文件夹中创建了一个虚拟活动,并在androidTest文件夹中的AndroidManifest文件中声明了该活动.

我的基本目的是通过使用framelayout容器将其放入虚拟活动来测试可重用片段.

AndroidManife文件夹里面的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.droid.test"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk
        android:minSdkVersion="18"
        tools:overrideLibrary="android.support.test.uiautomator.v18" />
    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.droid" />

    <application>
        <uses-library android:name="android.test.runner" />
        <activity
            android:name="com.droid.DummyActivityForTest"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

我的测试类TestWidgets.java

public class TestWidgets extends ActivityInstrumentationTestCase2<DummyActivityForTest> {
    private AppCompatActivity mActivity;

    public TestWidgets() {
        super(DummyActivityForTest.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        mActivity = getActivity();
    }

    @Test
    public void testAddSpecializationClick() {
        onView(withId(R.id.widgets_rv)).perform(
                RecyclerViewActions.actionOnItemAtPosition(4, click()));
        Assert.fail("Not Implemented");
    }
Run Code Online (Sandbox Code Playgroud)

当我运行我的测试类时,它抛出异常,

java.lang.RuntimeException: Could not launch activity
at android.support.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:373)
at android.test.InstrumentationTestCase.launchActivityWithIntent(InstrumentationTestCase.java:119)
at android.test.InstrumentationTestCase.launchActivity(InstrumentationTestCase.java:97)
at android.test.ActivityInstrumentationTestCase2.getActivity(ActivityInstrumentationTestCase2.java:104)
at com.practo.droid.home.TestWidgets.setUp(TestWidgets.java:48)
at junit.framework.TestCase.runBare(TestCase.java:132)
at junit.framework.TestResult$1.protect(TestResult.java:115)
at android.support.test.internal.runner.junit3.AndroidTestResult.runProtected(AndroidTestResult.java:77)
at junit.framework.TestResult.run(TestResult.java:118)
at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:55)
at junit.framework.TestCase.run(TestCase.java:124)
at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103)
at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:69)
at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
Caused by: java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=com.practo.droid/.DummyActivityForTest }
at android.app.Instrumentation.startActivitySync(Instrumentation.java:385)
at android.support.test.runner.MonitoringInstrumentation.access$201(MonitoringInstrumentation.java:90)
at android.support.test.runner.MonitoringInstrumentation$5.call(MonitoringInstrumentation.java:353)
at android.support.test.runner.MonitoringInstrumentation$5.call(MonitoringInstrumentation.java:350)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Run Code Online (Sandbox Code Playgroud)

我在Android测试方面没有太多经验,有人请帮忙提一些建议.

小智 13

在项目构建期间生成了2个APK.首先是APK与应用程序和第二个APK包含测试.如果您将活动放在测试文件夹中,它将位于用于测试的第二个APK中,而您的应用程序APK不包含它.这就是您收到此错误的原因(因为您的应用程序APK没有此类活动).

因此,唯一的方法是将您的活动不在测试文件夹中,而是放在源中.您可以创建多个应用程序变体(请参阅此处的详细信息),因此当您在生产中构建APK时,您的虚拟活动将不会包含在其中.