Android SDK:22 (L) UiAutomator 版本:'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
在单击按钮时,我正在尝试使用 Android 应用程序访问仪器
Boolean start = false;
start = startInstrumentation(new ComponentName("com.automation.vzw.sanity.test", "android.test.InstrumentationTestRunner"), null, null);
System.out.println("value of start is " +start);
Run Code Online (Sandbox Code Playgroud)
start 的值显示“true”,表示它已启动
问题是:一旦 Instrumentation 启动并调用 setUp() ,当下面被执行时,就会发生崩溃
public class ApplicationTest extends InstrumentationTestCase {
public void setUp() {
UiDevice testDevice = UiDevice.getInstance(getInstrumentation());
}
public void testCase1(){
System.out.println("In testcase1");
}
}
Run Code Online (Sandbox Code Playgroud)
崩溃详情:
/TestRunner(11209): started: testCase1(com.automation.vzw.sanity.ApplicationTest)
I/TestRunner(11209): failed: testCase1(com.automation.vzw.sanity.ApplicationTest)
I/TestRunner(11209): ----- begin exception -----
I/TestRunner(11209):
I/TestRunner(11209): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.UiAutomation.setOnAccessibilityEventListener(android.app.UiAutomation$OnAccessibilityEventListener)' on a null object …Run Code Online (Sandbox Code Playgroud) android ui-automation android-instrumentation android-uiautomator
我正在尝试在 Android Studio 中实现一个测试类来对 DBAdapter 进行一些测试。由于我需要在手机上运行测试才能使用数据库,因此我创建了一个 Instrumentation Unit Test(因为我尝试仅使用单元测试来完成它,但我需要使用数据库等等,并且那些只在本地运行)。
问题是当我尝试运行测试类时,使用我的手机作为运行设备,编译器抛出以下错误:
error: package org.junit does not exist
Run Code Online (Sandbox Code Playgroud)
我一直在寻找解决方案,但我没有找到。
这是我的测试类(只是骨架):
import org.junit.Test;
import android.support.test.runner.AndroidJUnit4;
import static org.junit.Assert.*;
public class DbAdapterTest {
@Test
public void testCreateSeries() throws Exception {
}
}
Run Code Online (Sandbox Code Playgroud)
这是 build.gradle 脚本:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.w2w.whattowatch"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests.returnDefaultValues = …Run Code Online (Sandbox Code Playgroud) junit android unit-testing android-studio android-instrumentation
我有两个项目:
在测试项目中,有一个测试类,用于测试导入和使用org.codehaus.jackson.JsonFactory类的库项目中的相应类。
在测试项目中,我在 maven build 中添加了依赖项:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
和
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
测试项目的AndroidManifest.xml是这样的:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.lib.test"
android:versionCode="1"
android:versionName="1.2" >
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.my.lib" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application>
<uses-library android:name="android.test.runner" />
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
当我运行仪器测试时,我的测试被执行,但在执行该特定测试类时,我总是收到以下运行时错误:
java.lang.ClassNotFoundException: Didn't find class "org.codehaus.jackson.JsonFactory" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.my.lib.test-2/base.apk", zip file "/data/app/com.my.lib-1/base.apk"],nativeLibraryDirectories=[/data/app/com.my.lib.test-2/lib/arm, /data/app/com.my.lib-1/lib/arm, /vendor/lib, /system/lib]]
07-06 18:06:08.041 24082 24998 E AndroidRuntime: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
07-06 18:06:08.041 24082 24998 …Run Code Online (Sandbox Code Playgroud) 我无法调试android Instrumentation测试用例(使用espresso和UiAutomator)
但早期的测试用例调试工作正常,但最近的更新它不再起作用了.它总是在实例化单元测试时受到攻击

我可以在命令窗口看到它正在尝试启动PACKAGE_NAME**.test**
adb shell am instrument -w -r -e debug true -e class PACKAGE_NAME.LanguageScreenTest#testSelectImportant PACKAGE_NAME**.test**/android.support.test.runner.AndroidJUnitRunner
Run Code Online (Sandbox Code Playgroud) android android-testing android-studio android-instrumentation
我的 android 测试包上有大约 25 个测试。我有一些要在集成过程中运行的测试,以及一些作为普通仪器测试运行的测试。我正在尝试使用 Gradle 任务调用集成测试,但仪器测试似乎不可能。我已经检查过单元测试,有可能是这样的——
task integrationTest(
type: Test,
description: 'Run integration tests only. Pass in \'-Pintegration=true\'',
dependsOn: ['testDebugUnitTest', 'clean'] ) {
//Here for task completion, not actually used since sub task of testDebugUnitTest
testClassesDir = file("src/integrationTest/java/");
classpath = files("$System.env.ANDROID_HOME/sources/android-18")
//
//Turn on integration testing when argument exists and is true
//
if (project.hasProperty('integration')) {
println integration
if (integration == 'true') {
integrationTests = true
}
}
}
Run Code Online (Sandbox Code Playgroud)
并做这样的事情
testOptions {
unitTests.all {
useJUnit()
if (integrationTests.toBoolean()) {
println …Run Code Online (Sandbox Code Playgroud) android automated-tests unit-testing android-gradle-plugin android-instrumentation
在Android上以主机模式访问USB设备的常用方法如下:
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
PendingIntent pi = PendingIntent.getBroadcast(...);
manager.requestPermission(driver.getDevice(), pi);
Run Code Online (Sandbox Code Playgroud)
这会导致系统显示一个弹出窗口,要求访问 USB 设备的权限 - 当用户单击“允许”时,我们的应用程序会通过 PendingIntent 收到通知。
但是如何在已检测的 JUnit 测试(在手机上运行)中获得此权限?
1) 我们可能会尝试在 @Before 方法中执行此操作 - 但由于权限请求是异步的,我们不能只是“等待”用户响应。
2)在android测试支持库中有GrantPermissionRule,可以用@Rule注解注入测试。但是我找不到“usb 主机”权限的标准 Android 规则名称!(可能没有)
那么,如何从设备上运行的 JUnit 测试请求以主机模式访问 USB 设备的权限?有没有有效的方法?
我想在我的仪器测试中使用大文件(> 2 GB zip 存档以及视频文件)来测试从 SD 卡/内部存储加载的文件。
我如何编写这些仪器测试并为其配备所需的文件?对于其他测试,我只需要非常小的文件,所以我将它们放在应用程序的原始资源中
InputStream rStream = context.getResources().openRawResource(R.raw.smalltestvideo);
Run Code Online (Sandbox Code Playgroud)
但现在我需要专门测试大文件,这不再是一种选择。我在跑步
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Run Code Online (Sandbox Code Playgroud) 我已经扩展了InputMethodService类来创建我的自定义IME.但是,我正在努力编写有效的Instrumentation测试用例来验证行为.以前Service,可以使用测试ServiceTestCase<YourServiceClass>.但它似乎已被弃用,新格式看起来像这样.现在在给定的指导方针中,我正在努力解决这个片段:
CustomKeyboardService service =
((CustomKeyboardService.LocalBinder) binder).getService();
Run Code Online (Sandbox Code Playgroud)
由于我正在扩展InputMethodService,它已经抽象了IBinder,我怎样才能LocalBinder获得这个代码片段的运行?目前,此代码段抛出以下异常:
java.lang.ClassCastException:android.inputmethodservice.IInputMethodWrapper无法强制转换为com.osrc.zdar.customkeyboard.CustomKeyboardService $ LocalBinder
扩展类看起来像:
public class CustomKeyboardService extends InputMethodService {
// Some keyboard related stuff
public class LocalBinder extends Binder {
public CustomKeyboardService getService() {
// Return this instance of LocalService so clients can call public methods.
return CustomKeyboardService.this;
}
}
// Some keyboard related stuff
}
Run Code Online (Sandbox Code Playgroud)
如何扩展我的自定义类,以便CustomKeyboardService service
= ((CustomKeyboardService.LocalBinder) binder).getService();不返回错误?
这是我的测试用例代码:
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest2 …Run Code Online (Sandbox Code Playgroud) android android-input-method servicetestcase android-service-binding android-instrumentation
我正在尝试在模拟器上运行 Detox 测试。当我运行“ detox test --configuration android.emu.debug -l verbose”时,它会抛出错误消息
“在设备 emulator-5556 上找不到用于包 my.project.android.debug 的检测运行程序
在 ADB.getInstrumentationRunner (../node_modules/detox/src/devices/android/ADB.js:219:54)"
注意:apk 正在模拟器中安装,但没有启动应用程序。
版本详情:
只是为了更新更多,
当我运行时,adb shell pm list instrumentation我找不到我的应用程序检测。但是,该应用程序已安装在模拟器中。
我有一个包含 2 个模块的 android 项目。
我已经为两个项目设置了所需的测试依赖项,Gradle 配置相同。
当我从演示应用程序运行测试时,它运行没有任何问题。但是当从库模块运行相同的示例测试代码时,它无法找到测试。
这是我可以从 android studio 的“运行”选项卡中看到的日志。
演示模块测试 - 结果
$ adb shell CLASSPATH=$(pm path androidx.test.services) app_process / androidx.test.services.shellexecutor.ShellMain am instrument -r -w -e targetInstrumentation com.cardstream.paymentdemo.test/androidx.test.runner.AndroidJUnitRunner -e debug false -e class 'com.cardstream.paymentdemo.ExampleInstrumentedTest' androidx.test.orchestrator/androidx.test.orchestrator.AndroidTestOrchestrator
Client not ready yet..
Started running tests
Tests ran to completion.
Run Code Online (Sandbox Code Playgroud)
库模块测试 - 结果
$ adb shell CLASSPATH=$(pm path android.support.test.services) app_process / android.support.test.services.shellexecutor.ShellMain am instrument -r -w -e targetInstrumentation com.cardstream.payment.test/androidx.test.runner.AndroidJUnitRunner -e debug false -e class 'com.cardstream.payment.ExampleInstrumentedTest' android.support.test.orchestrator/android.support.test.orchestrator.AndroidTestOrchestrator
Client …Run Code Online (Sandbox Code Playgroud) android unit-testing android-espresso android-instrumentation androidx