问题描述
ExampleInstrumentedTest 这个类有什么特殊的吗?为什么所有测试方法都报错,除了在useAppContext()里的方法有效果?
在这个类的其他方法测试
@Test public void testSOmething() throws Exception {assertEquals('a', 'a'); }
运行结果是
$ adb shell am instrument -w -r -e debug false -e class com.aa.safe.locked.ExampleInstrumentedTest#testSOmething com.aa.safe.locked.test/android.test.InstrumentationTestRunnerClient not ready yet..Started running testsTest running failed: Unable to find instrumentation info for: ComponentInfo{com.aa.safe.locked.test/android.test.InstrumentationTestRunner}Empty test suite.
而在这个类自带的方法中测试
@Test public void useAppContext() throws Exception {// Context of the app under test.//Context appContext = InstrumentationRegistry.getTargetContext(); assertEquals('a', 'a');//assertEquals('com.aa.safe.locked', appContext.getPackageName()); }
结果是成功
$ adb shell am instrument -w -r -e debug false -e class com.aa.safe.locked.ExampleInstrumentedTest#useAppContext com.aa.safe.locked.test/android.support.test.runner.AndroidJUnitRunnerClient not ready yet..Started running testsTests ran to completion.然后我想在自己包下建一个测试类进行测试
我建立了一个类继承AndroidTestCase, 然后在清单文件里进行配置,但是在配置的过程中发现
<instrumentationandroid:name='android.test.InstrumentationTestRunner'android:targetPackage='com.aa.safe.locked' > </instrumentation>
在targetPackage这里,包名提示是错误的,即使它和我的
<manifest xmlns:android='http://schemas.android.com/apk/res/android' package='com.aa.safe.locked'>
package是一样的,也提示错误. ### 请教一下 studio 单元测试应该怎么操作?
问题解答
回答1:可以使用Robolectric框架 ,进行单元测试!