14

我在测试我的 android 应用程序时遇到了问题。
我有 2 个 testCase 类,如果我单独执行它们,没有问题,测试运行到最后。但是,如果我在我的测试项目上“右键单击”并选择“Run as Android Junit Test”,我会收到一条消息

 Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
 [2012-03-27 15:56:27 - matroussedemaquillageTest] Collecting test information
 [2012-03-27 15:56:31 - matroussedemaquillageTest] Test run failed: Instrumentation run  failed due to 'Process crashed.'

我的两个测试类见下文:

第一个测试班

package fr.smardine.matroussedemaquillage.test;

import android.test.ActivityInstrumentationTestCase2;
import android.widget.ImageSwitcher;
import fr.smardine.matroussedemaquillage.EntryPoint;

public class EntryPointTest extends
    ActivityInstrumentationTestCase2<EntryPoint> {

private EntryPoint mActivity;
    private ImageSwitcher mSwitcher;

    public EntryPointTest() {
        super("fr.smardine.matroussedemaquillage",
                fr.smardine.matroussedemaquillage.EntryPoint.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void test2() {
        assertEquals(2, 2);
    }
}

第二个:

package fr.smardine.matroussedemaquillage.test;

import android.test.ActivityInstrumentationTestCase2;
import android.widget.ImageView;
import fr.smardine.matroussedemaquillage.Main;

public class MainTest extends ActivityInstrumentationTestCase2<Main> {

    private Main mActivity;
    private ImageView btRemplir;
    private ImageView btPerime;
    private ImageView btNotes;

    public MainTest() {
        super("fr.smardine.matroussedemaquillage",
                fr.smardine.matroussedemaquillage.Main.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }


    public void test1() {
        assertEquals(1, 1);
    }
}

正如您所看到的,我的测试并没有那么复杂,即使我在启动模拟器时“擦除用户数据”,如果我执行这两个测试,也会出现相同的消息。
哦,顺便说一下,模拟器在 android 2.1 下运行,这是我的 AndroidManifest.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fr.smardine.matroussedemaquillage.test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7" />

<instrumentation
    android:name="android.test.InstrumentationTestRunner"
    android:targetPackage="fr.smardine.matroussedemaquillage" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <uses-library android:name="android.test.runner" />
</application>

</manifest>

编辑:我的日志猫:

I/ActivityManager(64): Start proc fr.smardine.matroussedemaquillage for added application fr.smardine.matroussedemaquillage: pid=510 uid=10029 gids={3003, 1015} 
D/ddm-heap(510): Got feature list request D/dalvikvm(510): GC freed 5427 objects / 420224 bytes in 90ms 
D/dalvikvm(510): GC freed 6498 objects / 506616 bytes in 79ms 
D/dalvikvm(510): GC freed 7048 objects / 567464 bytes in 90ms 
D/dalvikvm(510): GC freed 8628 objects / 503840 bytes in 73ms 
I/System.out(510): Failed to open test.properties 
I/AndroidRuntime(510): AndroidRuntime onExit calling exit(-1) –
D/Zygote(30): Process 510 exited cleanly (255) 
I/ActivityManager(64): Process fr.smardine.matroussedemaquillage (pid 510) has died. 
W/ActivityManager(64): Crash of app fr.smardine.matroussedemaquillage running instrumentation ComponentInfo{fr.smardine.matroussedemaquillage.test/android.test.Instrumentatio‌​nTestRunner} 
D/ActivityManager(64): Uninstalling process fr.smardine.matroussedemaquillage
D/AndroidRuntime(504): Shutting down VM 
D/dalvikvm(504): DestroyJavaVM waiting for non-daemon threads to exit 
D/dalvikvm(504): DestroyJavaVM shutting VM down
D/dalvikvm(504): HeapWorker thread shutting down 
D/dalvikvm(504): HeapWorker thread has shut down 
D/jdwp(504): JDWP shutting down net... 
D/jdwp(504): Got wake-up signal, bailing out of select 
I/dalvikvm(504): Debugger has detached; object registry had 1 entries 
D/dalvikvm(504): VM cleaning up 
D/dalvikvm(504): LinearAlloc 0x0 used 643668 of 5242880 (12%) 
I/dalvikvm(504): JNI: AttachCurrentThread (from ???.???) 
E/AndroidRuntime(504): ERROR: thread attach failed'
4

4 回答 4

3

当我在测试活动的 onFinish 上使用 System.exit(0) 时,我曾经收到此错误,如下所示:

@Override
    public void finish() {
        super.finish();
        System.exit(0);

    }

因此,请检查您的 Main 活动的 onFinish 方法。

于 2012-10-12T11:32:12.537 回答
1

I encountered this error as well. However, I ended up finding that my test suite did run, the only problem was that an assert in my test code failed.

I looked into LogCat and filter out those Tagged "TestRunner" messages and found the assertion failure message among other testing logs.

于 2013-04-22T18:49:23.280 回答
1

我在运行我的 android 仪器时也遇到了类似的问题。最终我得出的结论是问题出在System.exit(0)

现在,原因,它导致问题的原因根据其文档

导致 VM 停止运行并退出程序。

System.exit(0)被执行时,在此代码之后编写的所有其他代码都被跳过并且活动类被最终确定并进入垃圾收集。由于 Instrumentation 依赖于活动生命周期方法,因此活动类是垃圾收集的,如果对象本身不存在,则没有机会调用其方法。

因此,如果要对应用程序进行单元测试,请避免使用 System.exit(0),而是使用finish()方法。

于 2014-08-04T11:14:36.423 回答
0

万一其他人也在使用 Robotium 并看到错误:我忘记tearDown了打开的活动,这导致了上述错误。

public void tearDown() throws Exception {
    solo.finishOpenedActivities();
}
于 2016-01-05T16:47:43.567 回答