标签: android-testing

使用Espresso Null指针测试多个活动

我正在进行一项测试,让我参加另一项活动.当我到达那里时,我需要等待对话消失.

  public class StressTest extends ActivityInstrumentationTestCase2<DashboardActivity> {

DashboardActivity activity;
ConsoleActivity consoleActivity;


public StressTest() {
    super(DashboardActivity.class);
}

public void setUp() throws Exception {
    super.setUp();
    activity = getActivity();
}

public void testRun() throws InterruptedException {
    schedule();
    quickstart();
    IP.enterIP();
<-----------FAILS HERE FROM A NPE------------------->
    while (consoleActivity.getConnectDialog() != null && consoleActivity.getConnectDialog().isShown()){
        Thread.sleep(
    } 
}
Run Code Online (Sandbox Code Playgroud)

如你所见,我正在开始DashboardActivity.然后进入ConsoleActivity,我需要检查ConnectDialog.如果没有获得NPE,我怎么能这样做?

编辑

 public void testRun() throws InterruptedException {
    schedule();
    quickstart();
    IP.enterIP();
    Thread.sleep(500);

    getInstrumentation().runOnMainSync(new Runnable() {
        @Override
        public void run() {
            Collection<Activity> resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED); …
Run Code Online (Sandbox Code Playgroud)

android nullpointerexception android-testing

3
推荐指数
1
解决办法
2678
查看次数

Handler.handleMessage在测试中未调用,但在应用程序中调用

我有一个在单独进程中运行的服务.该服务在onCreate()方法中生成一个新线程.该线程将消息发送回服务.

如果我手动启动应用程序一切正常 - Handler我的服务收到消息.但在我的测试handleMessage()方法中永远不会被调用.

如何修复测试以使handleMessage()方法有效?

谢谢!

服务:

public class MyService extends Service {

    private ServiceHandler mHandler;
    private final int MSG_HELLO_WORLD = 1;

    private final String TAG = getClass().getSimpleName();

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return null;
    }


    @Override
    public void onCreate() {
        Log.d(TAG, "MyService.onCreate");
        super.onCreate();

        mHandler = new ServiceHandler();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "Thread: run()");

                while (true) { …
Run Code Online (Sandbox Code Playgroud)

java android android-service android-testing android-handler

3
推荐指数
1
解决办法
2833
查看次数

在AVD仿真器上运行Gradle connectedCheck时出现NoClassDefFoundError

我正在尝试为Android应用运行Espresso测试.它在硬件设备上运行良好.当我在新创建的AVD模拟器上运行它时,它会失败,如下所示:

:ExampleApp:connectedDebugAndroidTest

com.example.MainFragmentTest > initializationError[Nexus_5_API_19(AVD) - 4.4.2] 
FAILED 
    java.lang.NoClassDefFoundError: com/example/MainActivity
    at java.lang.Class.getDeclaredFields(Native Method)
:ExampleApp:connectedDebugAndroidTest FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':ExampleApp:connectedDebugAndroidTest'.
> There were failing tests. See the report at: 
file:///home/user/work/ExampleApp/build/reports/androidTests/connected/index.html
Run Code Online (Sandbox Code Playgroud)

HTML报告包含以下堆栈跟踪:

java.lang.NoClassDefFoundError: com/example/MainActivity
at java.lang.Class.getDeclaredFields(Native Method)
at java.lang.Class.getDeclaredFields(Class.java:610)
at org.junit.runners.model.TestClass.getSortedDeclaredFields(TestClass.java:77)
at org.junit.runners.model.TestClass.scanAnnotatedMembers(TestClass.java:70)
at org.junit.runners.model.TestClass.<init>(TestClass.java:57)
at org.junit.runners.ParentRunner.createTestClass(ParentRunner.java:88)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:83)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at android.support.test.internal.runner.junit4.AndroidJUnit4ClassRunner.<init>(AndroidJUnit4ClassRunner.java:38)
at android.support.test.runner.AndroidJUnit4.<init>(AndroidJUnit4.java:36)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.buildAndroidRunner(AndroidAnnotatedBuilder.java:57)
at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.runnerForClass(AndroidAnnotatedBuilder.java:45)
at …
Run Code Online (Sandbox Code Playgroud)

android android-virtual-device android-emulator android-testing android-espresso

3
推荐指数
1
解决办法
1616
查看次数

使用any()时出错; 在Android测试中需要不兼容的类型:Matcher <View> found:Matcher <Object>

我运行下面的代码并返回任何错误();

error: incompatible types
required: Matcher <View>
found:    Matcher <Object>

/** 
     * Perform action of waiting until UI thread is free. <p/> E.g.: onView(isRoot()).perform(waitUntilIdle());
     * @return
     */
    public static ViewAction waitUntilIdle(){
      return new ViewAction(){
        @Override public Matcher<View> getConstraints(){
          return anything();
        }
        @Override public String getDescription(){
          return "wait until UI thread is free";
        }
        @Override public void perform(    final UiController uiController,    final View view){
          uiController.loopMainThreadUntilIdle();
        }
      }
    ;
    }
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

testing android android-testing android-espresso

3
推荐指数
1
解决办法
650
查看次数

JUnit 测试 if else case

如何将测试写入当前方法?我使用 jUnit 4。

   public void setImage() {
        if(conditionOne){
            myView.setImageOne();
        } else {
            myView.setImageTwo();
        }
    }
Run Code Online (Sandbox Code Playgroud)

java testing junit junit4 android-testing

3
推荐指数
1
解决办法
3万
查看次数

在开始Espresso测试之前如何准备数据库数据?

数据库:SQLite

表:联系方式

浓咖啡测试:

@Test
public void testBlock() {
   onData(anything()).inAdapterView(withId(R.id.container_ListView)).atPosition(0).onChildView(withText(R.string.block_user))
            .perform(click());
}
Run Code Online (Sandbox Code Playgroud)

并且测试成功通过。但是,只有在开始联系之前状态已解除阻止(数据库表中的列状态)时,它才会成功。因此,我需要(开始测试之前)将此联系人更新为unblock状态。我怎样才能做到这一点?还是有人对此有更好的解决方案?

android android-testing android-espresso

3
推荐指数
1
解决办法
1379
查看次数

Android项目中的测试apk在哪里?

对于正在运行的测试,Android Studio / Gradle会生成一个测试apk,该apk在应用apk上执行测试。任何人都可以共享生成测试apk的位置以及项目内部的位置吗?

以下是我的app / build.gradle

apply plugin: 'com.android.application'

android {
   compileSdkVersion 25
   buildToolsVersion "25.0.2"
   defaultConfig {
      applicationId "com.ark.beacons_test"
      minSdkVersion 16
      targetSdkVersion 25
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }
   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
   }

   packagingOptions {
      exclude 'META-INF/LICENSE'
   }

}

dependencies {
   compile fileTree(include: ['*.jar,*.aar'], dir: 'libs')
   compile(name: 'android-lib', ext: 'aar')
   compile 'com.android.support:appcompat-v7:25.3.1'
   compile 'com.android.support.constraint:constraint-layout:1.0.2' 
   androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
   })
   testCompile 'junit:junit:4.12'
}
Run Code Online (Sandbox Code Playgroud)

android android-testing

3
推荐指数
3
解决办法
3778
查看次数

我们如何自动只在较新的 Android 版本上运行测试?

我发现一项测试,尤其是在 KitKat 之前的设备上一直失败。我相信这与旧 Android 设备上使用的嵌入式 WebView 的变化有关(但也许这是错误的)。无论如何,回到我的问题:我想要一种优雅的方式来控制是否根据设备上运行的 Android 版本运行测试。

目前,如果运行时早于 KitKat,我会使用短路测试并通过测试的代码。这是相关的代码:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
  assertTrue("Skipping this test as the Android version is too low.", true);
  return;
}
Run Code Online (Sandbox Code Playgroud)

因为我已经依次尝试使用两个注解:@TargetApi@RequiresApi

@Test
@RequiresApi(Build.VERSION_CODES.KITKAT)
public void zimTest() {
    Log.v("kiwixTesting", "Running zimTest() on Android version: " + Build.VERSION.SDK_INT);
    ...
}
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,测试都在我的测试设备(包括 Android 4.3、4.3、4.4 和更新版本)上运行。我可以判断,因为测试运行程序显示测试已成功运行,并且日志中Log.v("kiwixTesting", "Running zimTest() on Android version: " + Build.VERSION.SDK_INT);出现以下日志消息的输出 。

完整代码在这里,我在这里跟踪工作

有人可以建议比我更好的方法吗?谢谢你。

android android-testing android-espresso

3
推荐指数
1
解决办法
355
查看次数

为 Facebook screenshot-tests-for-android 设置 gradle 插件的问题

我需要为android实现屏幕截图测试。

我已按照https://facebook.github.io/screenshot-tests-for-android/#gradle-setup上的步骤进行操作,并在我的 gradle 文件中包含以下内容

buildscript {
    // ...
    dependencies {
      // ...
      classpath 'com.facebook.testing.screenshot:plugin:0.9.0'
    }
  }

  apply plugin: 'com.facebook.testing.screenshot'
Run Code Online (Sandbox Code Playgroud)

但是,我收到一个错误

评估根项目“app-android”时出现问题。未能应用插件 [id 'com.facebook.testing.screenshot'] 找不到名称为“androidTestImplementation”的配置。

我的 build.gradle 中有以下内容

androidTestImplementation 'androidx.test:core:1.1.0'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation 'androidx.multidex:multidex-instrumentation:2.0.0'
Run Code Online (Sandbox Code Playgroud)

我找不到任何证据表明我需要其他任何东西。

我也在https://github.com/facebook/screenshot-tests-for-android 上看到

你需要 python-2.7 才能让 gradle 插件工作,我们还建议安装 python-pillow 库,它是记录和验证屏幕截图所需的。

我已经安装了它并看到我有 2.7.16 版我还安装了枕头作为推荐(v6.0.0)

android android-testing

3
推荐指数
1
解决办法
476
查看次数

无法访问 includeAndroidResource 在 UnitTestOptions 中是私有的

我正在尝试Kotlin Gradle ScriptAndroid Testing

当我尝试为测试配置 android 资源时,出现以下错误:

无法访问 includeAndroidResource 在 UnitTestOptions 中是私有的。

在此处输入图片说明

在 Android 单元测试中启用使用android Resource的正确方法是什么

android android-testing build.gradle android-gradle-plugin gradle-kotlin-dsl

3
推荐指数
1
解决办法
648
查看次数