Junit 为什么我不能在android测试项目中运行所有测试?

Junit 为什么我不能在android测试项目中运行所有测试?,junit,android-testing,Junit,Android Testing,现在,我在单独的测试项目中编写android测试代码来测试应用程序。我编写了许多测试用例和类。现在,我想写一套测试服。运行所有测试。但它有一个异常。代码如下: public static Test suit () { return new TestSuiteBuilder(AllTest.class) .includeAllPackagesUnderHere() .build(); } 例外情况如

现在,我在单独的测试项目中编写android测试代码来测试应用程序。我编写了许多测试用例和类。现在,我想写一套测试服。运行所有测试。但它有一个异常。代码如下:

 public static Test suit () {
        return new TestSuiteBuilder(AllTest.class)
                  .includeAllPackagesUnderHere()
                  .build();
    }
例外情况如下:

 public static Test suit () {
        return new TestSuiteBuilder(AllTest.class)
                  .includeAllPackagesUnderHere()
                  .build();
    }
junit.framework.AssertionFailedError:在com.netqin.myproject.test.alltest.alltest中未找到任何测试 在android.test.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)


怎么了,我找不出原因。感谢您的帮助。

方法includealPackageSunderHere()需要能够从保存测试套件的包或任何子包()中拉入测试

因此,您需要创建一个单独的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
    }
}
package com.example.app.tests;
导入android.test.ActivityInstrumentationTestCase2;
公共类MyTestMethods扩展了ActivityInstrumentationTestCase2{
公共MyTestMethods(){
super(“com.example.app”,即您正在测试的活动.class);
}
受保护的void setUp()引发异常{
super.setUp();
}
受保护的void tearDown()引发异常{
super.tearDown();
}
public void testFirstTest(){
测试代码在这里
}
公共void testSecondTest(){
测试代码在这里
}
}

在本例中,testFirstTest()和testSecondTest()将包含在测试套件(MyTestSuite.class)中。作为Android JUnit测试运行MyTestSuite.java现在将运行这两个测试。

此处可能存在键入错误。MyTestSuite必须是“公共静态测试套件(){”,而不是“公共静态测试套件(){”(套件vs套件)。