2012-11-26 141 views
2

現在,我在單獨的測試項目中編寫android測試來測試應用程序。我編寫了很多測試用例和類。現在,我想寫一個測試服。運行所有的測試,但它有一個例外。該代碼是如下:爲什麼我不能在android測試項目中運行所有的測試?

public static Test suit() { 
     return new TestSuiteBuilder(AllTest.class) 
        .includeAllPackagesUnderHere() 
        .build(); 
    } 

的例外是如下:

junit.framework.AssertionFailedError:沒有在android.test在com.netqin.myproject.test.alltest.AllTest 試驗發現。 AndroidTestRunner.runTest(AndroidTestRunner.java:190) 在android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175) 在android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) 在android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1584)

什麼是錯,我無法找出原因。任何幫助都是感恩。

回答

0

includeAllPackagesUnderHere()方法需要能夠從保存測試套件的包或任何子包(link)中抽取測試。

因此,您需要創建一個單獨的JUnit測試用例,它實際上包含您的測試方法在同一個包中。例如,您可能有兩個文件:

1)MyTestSuite.java

package com.example.app.tests; 

import junit.framework.Test; 
import junit.framework.TestSuite; 
import android.test.suitebuilder.TestSuiteBuilder; 

public class MyTestSuite extends TestSuite { 

    /** 
    * A test suite containing all tests 
    */ 
    public static Test suit() { 
     return new TestSuiteBuilder(MyTestSuite.class) 
        .includeAllPackagesUnderHere() 
        .build(); 
    } 

} 

注意:確保內TestSuiteBuilder類,在這種情況下MyTestSuite.class,匹配包含類的名字,這種情況MyTestSuite。

2)MyTestMethods.java

package com.example.app.tests; 

import android.test.ActivityInstrumentationTestCase2; 

public class MyTestMethods extends ActivityInstrumentationTestCase2<TheActivityThatYouAreTesting> { 

    public MyTestMethods() { 
     super("com.example.app",TheActivityThatYouAreTesting.class); 
    } 

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

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

    public void testFirstTest(){ 
     test code here 
    } 

    public void testSecondTest(){ 
     test code here 
    } 
} 

在這種情況下,testFirstTest()和testSecondTest()將被包含在您的測試套件(MyTestSuite.class)。運行MyTestSuite.java作爲Android JUnit測試現在將運行這兩個測試。

+2

可能的錯字在這裏。 MyTestSuite必須是「公共靜態測試套件(){」而不是「公共靜態測試套裝(){」(套件與套裝)。 –

相關問題