2012-07-24 67 views
12

嘗試啓動我的應用程序的JUnit測試(robotium)。測試運行失敗:測試運行未能完成。預計1測試,收到0

public class MainTest extends ActivityInstrumentationTestCase2<MainActivity> { 
    private Solo solo; 

    public MainTest() { 
     super("nix.android.contact", MainActivity.class);// TODO Auto-generated constructor stub 
    } 

    protected void setUp() throws Exception { 
     super.setUp(); 
     solo = new Solo(getInstrumentation(), getActivity()); 
    } 

    public void AddContact() { 
     solo.assertCurrentActivity("main", MainActivity.class); 
    } 
} 

清單

<instrumentation 
    android:name="android.test.InstrumentationTestRunner" 
    android:targetPackage="nix.android.contact" /> 

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

When I try run test, get in console:

Test run failed: Test run failed to complete. Expected 1 tests, received 0 

I try create other test for other app (very simple app) - works.

Thanks

回答

1

The problem is in your call at

super("nix.android.contact", MainActivity.class); 

In my code I have

super("nix.android.contact", Class.forName("nix.android.contact.MainActivity")); 

I've also done it this way without have to name the Generic for the ActivityInstrumentationTestCase 2

public class TestApk extends ActivityInstrumentationTestCase2 { 

    private static final String TARGET_PACKAGE_ID = "nix.android.contact"; 
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "nix.android.contact.MainActivity"; 

    private static Class<?> launcherActivityClass; 
    static{ 
      try { 
        launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME); 
      } catch (ClassNotFoundException e) { 
        throw new RuntimeException(e); 
      } 
    } 

    @SuppressWarnings("unchecked") 
    public TestApk() throws ClassNotFoundException { 
      super(TARGET_PACKAGE_ID, launcherActivityClass); 
    } 
1

I had the same error message. The problem was that my test method name needed to start with 'test'.

Eg: testMethod1()作品,同時method1Test()給出了錯誤。

+0

沒錯。所有的方法都必須帶有前綴「test」的名稱! – Alex 2012-10-20 19:54:08

+0

這是個玩笑嗎? – Janosch 2018-01-05 10:40:30

5

我有這個問題,當我沒有一個無參數的構造函數。

public class MainActivityTest extends 
    ActivityInstrumentationTestCase2<MainActivity_> { 

public MainActivityTest() { 
    super(MainActivity_.class); 
} 
... 
-2

所有測試名稱都應以「test」前綴開頭。

0

檢查你沒有推出一個你的測試的構造函數依賴的方法,但應用程序沒有使用任何方法--logcat會抱怨應用程序包中缺少類或方法。

嘗試卸載目標軟件包,以檢查它是否來自替代版本(例如,如果您在Eclipse中使用Maven)。

0

我有這個錯誤,我固定它去除構造方法的參數,它是由Eclipse的嚮導創建爲:

public OptionsActivityTest(String name) { 

我不得不刪除「字符串名稱」,使我的測試工作再次。

相關問題