Android Studio中的测试工作正常但是通过命令行它无法正常工作。
1.我在Android Studio 1.3.1中构建了项目,打开了虚拟设备和cmd
2. adb install C:\...\build\outputs\apk\Audioteka-debug.apk
3. adb shell pm list instrumentation //there is instrumentation definded in Android Manifest
4. adb shell am instrument -w pl.k2.droidoaudioteka/android.test.InstrumentationTestRunner
结果:
Test results for InstrumentationTestRunner=
Time: 0.001
OK (0 tests)
事实证明,在apk中dex中没有测试类。
我的测试在同一个项目中,结构如下:
-src
|
+--main
| |
| +--java
| | |
| | +--pl.k2.droidoaudioteka
| | \--AudiotekaApplication.java
| |
| \--AndroidManifest.xml
|
+--test
|
+--java
|
+--pl.k2.droidoaudioteka
|
\--_1_2_0_LaunchAfterAppInstallationTest.java
|
\--_1_2_1_...
我已添加到清单
<manifest>
....
<application>
...
<uses-library android:name="android.test.runner" />
</application>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:label="RobotiumTest"
android:targetPackage="pl.k2.droidoaudioteka" />
</manifest>
在gradle中我有
android {
...
defaultConfig {
minSdkVersion 14
targetSdkVersion 14
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
sourceSets {
main {
java.srcDirs = ['src/main/java']
aidl.srcDirs = ['src/main/java']
androidTest.setRoot('src/test')
}
}
...
}
dependencies {
...
androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.3.1'
}
,典型的测试类看起来像这样:
package pl.k2.droidoaudioteka;
import android.test.ActivityInstrumentationTestCase2;
import com.robotium.solo.Solo;
/**
* Created by Admin on 2015-05-12.
*/
public class _1_2_0_LaunchAfterAppInstallationTest extends ActivityInstrumentationTestCase2 {
private Solo solo;
private long startTime;
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "pl.k2.droidoaudioteka.ui.views.impl.smartphone.WelcomeActivity";
private static Class<?> launcherActivityClass;
static{
try {
launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public _1_2_0_LaunchAfterAppInstallationTest() throws ClassNotFoundException {
super(launcherActivityClass);
}
public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation());
getActivity();
}
@Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}
public void testRun() {
/* During first launch after app installation, verify if the screen with flags of countries and slider is presented.
Flags represent local stores.
*/
//test code here
}
}
答案 0 :(得分:0)
使用命令构建
gradlew.bat clean assembleDebug test
完成了这项工作