标签: android-testing

Android gradle测试任务

有人可以解释这些类型的Android gradle任务之间的主要区别吗?

  • check - 运行所有检查.
  • connectedAndroidTest - 在连接的设备上安装并运行Build'debug'的测试.
  • connectedCheck - 在当前连接的设备上运行所有设备检查.
  • deviceCheck - 使用设备提供程序和测试服务器运行所有设备检查.

例如,如果我跑connectedAndroidTestconnectedCheck,似乎一切都将是相同的- >我的测试类被称为(设备或仿真器上).

android android-testing

10
推荐指数
1
解决办法
2925
查看次数

切换到AndroidJUnitRunner后,现有的Android UI测试停止工作

我们必须围绕我们的照相机功能的一些UI测试,之后我们从做了开关InstrumentationTestRunner,以AndroidJUnitRunner作为我们移动到咖啡/ JUnit4的一部分,我们再也不能可靠地运行我们现有的测试,由于频繁的RuntimeException当我们调用getActivity():

java.lang.RuntimeException: Could not launch intent Intent { flg=0x14000000 cmp=com.cookbrite.dev/com.cookbrite.ui.ReceiptCaptureActivity (has extras) } within 45 seconds. Perhaps the main thread has not gone idle within a reasonable amount of time? There could be an animation or something constantly repainting the screen. Or the activity is doing network calls on creation? See the threaddump logs. For your reference the last time the event queue was idle before your activity launch request was 1434471981236 and …
Run Code Online (Sandbox Code Playgroud)

android robotium android-testing

10
推荐指数
1
解决办法
1866
查看次数

如何使用Espresso意图存根选择图像意图?

这是我第一次使用espresso意图,我按照Android测试项目中提供的IntentsBasicSamle但没有解决.

在我的应用程序中,我有一个活动,用户选择多个图像然后返回的图像显示在gridview中,现在通过使用espresso意图我想模拟这个而不去实际的图像选择器并每次返回一些指定的图像.

没有错误,但在我运行测试时仍会打开图像选择器窗口.我觉得我做错了,但我没有得到它是如何工作的.

我正在测试使用apk.

这就是照片选择器的调用方式

应用代码

这就是我调用选择图像的方式Intent和结果在onActivityResult中处理.

if (ApiUtils.checkApiLevel(18)) {
        //API18+
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    }
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,      getActivity().getString(R.string.fragment_image_selection_select_picture)), 1);
Run Code Online (Sandbox Code Playgroud)

测试代码

@RunWith(AndroidJUnit4.class)
@LargeTest
public class ImagesTests {

private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.company.recorder.RecorderActivity";
private static Class<? extends Activity> activityClass;
private ServiceValidatorIdlingResource serviceValidatorIdlingResource;

static {
    try {
        activityClass = (Class<? extends Activity>) Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}

/**
 * Change espresso default view idling time
 * so that test can wait for long time …
Run Code Online (Sandbox Code Playgroud)

java android android-intent android-testing android-espresso

10
推荐指数
1
解决办法
4748
查看次数

使用不同的Application类测试Android Activity

我正在寻找一种方法来测试一个使用JUnit4和ActivityTestRule的Activity,但是使用不同的应用程序类(例如模拟或继承).我能够使用清单合并和工具来获取这个项目:在androidTest/AndroidManifest.xml中的application标签上替换="android:name".然而,这不适用于应用程序.

有什么想法可以做到这一点?

android android-testing android-espresso

10
推荐指数
1
解决办法
3562
查看次数

Android Test Orchestrator无法使用Android X.

我最近迁移了我的项目以使用AndroidX,并使用以下文档在gradle上为我的espresso测试配置了测试编排器:

https://developer.android.com/training/testing/junit-runner#using-android-test-orchestrator

我有依赖:

androidTestUtil 'androidx.test:orchestrator:1.1.0-beta01'
Run Code Online (Sandbox Code Playgroud)

但是,我没有执行任何测试,看起来他们在运行gradle时失败运行以下adb shell命令,即:

adb shell 'CLASSPATH=$(pm path android.support.test.services) app_process / \
  android.support.test.services.shellexecutor.ShellMain am instrument -w -e \
  targetInstrumentation com.example.test/androidx.test.runner.AndroidJUnitRunner \
  android.support.test.orchestrator/.AndroidTestOrchestrator'
Run Code Online (Sandbox Code Playgroud)

从上面看:似乎它试图用android支持版本而不是androidx版本执行此命令.

似乎没有记录什么用于androidx.

android android-testing android-espresso androidx androidx-test

10
推荐指数
1
解决办法
2284
查看次数

Espresso测试失败:想要匹配1个意图,实际匹配2个意图

我在使用espresso测试活动启动时遇到此错误.

android.support.test.espresso.base.DefaultFailureHandler $ AssertionFailedWithCauseError:想要匹配1个意图.实际上匹配了2个意图.

令人惊讶的是,相同代码的其他活动启动测试正在通过.

@RunWith(AndroidJUnit4.class)
public class HomeActivityTest {

  @Rule
  public final IntentsTestRule<HomeActivity> mHomeActivityRule = new IntentsTestRule<HomeActivity>(HomeActivity.class);


  @Test
  public void testFundTransferActivityStarted() {

    onView(withId(R.id.button_fund_transfer)).perform(click());

    intended(hasComponent("mypackage.FundTransferActivity"));
 }

}
Run Code Online (Sandbox Code Playgroud)

按钮单击只需使用startActivity启动FundTransfer活动.

android android-testing android-espresso

9
推荐指数
1
解决办法
2685
查看次数

如何确保在 Android 单元测试中调用 ViewModel#onCleared?

这是我的 MWE 测试类,它依赖于 AndroidX、JUnit 4 和 MockK 1.9:

class ViewModelOnClearedTest {
    @Test
    fun `MyViewModel#onCleared calls Object#function`() = mockkObject(Object) {
        MyViewModel::class.members
            .single { it.name == "onCleared" }
            .apply { isAccessible = true }
            .call(MyViewModel())

        verify { Object.function() }
    }
}

class MyViewModel : ViewModel() {
    override fun onCleared() = Object.function()
}

object Object {
    fun function() {}
}
Run Code Online (Sandbox Code Playgroud)

注意:该方法在超类中受保护ViewModel

我想验证MyViewModel#onCleared调用Object#function. 上面的代码通过反射实现了这一点。我的问题是:我可以以某种方式运行或模拟 Android 系统以便onCleared调用该方法,这样我就不需要反射吗?

来自onClearedJavaDoc:

当这个 ViewModel 不再使用时会调用这个方法,并且会被销毁。

那么,换句话说,我如何创建这种情况,以便我知道onCleared被调用并且我可以验证它的行为?

android unit-testing android-testing android-viewmodel android-architecture-components

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

JetPack Compose 现在支持 Robolectric 测试 – 但不起作用

自beta09起,Robolectric 测试据称现在已由 Jetpack Compose 支持。但我尝试实现一个非常简单的测试(AOSP 中功能的测试,请参阅 参考资料runClickScenario()),但它对我不起作用。我得到的错误是:

java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.lang.IllegalAccessException: class androidx.test.espresso.base.ThreadPoolExecutorExtractor$2 cannot access a member of class androidx.loader.content.ModernAsyncTask with modifiers "public static final"
java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.lang.IllegalAccessException: class androidx.test.espresso.base.ThreadPoolExecutorExtractor$2 cannot access a member of class androidx.loader.content.ModernAsyncTask with modifiers "public static final"
    at androidx.test.espresso.Espresso.onIdle(Espresso.java:15)
    at androidx.test.espresso.Espresso.onIdle(Espresso.java:1)
    at androidx.compose.ui.test.junit4.android.EspressoLink_androidKt.runEspressoOnIdle(EspressoLink.android.kt:91)
    at androidx.compose.ui.test.junit4.RobolectricIdlingStrategy$runUntilIdle$1.invoke(RobolectricIdlingStrategy.android.kt:69)
    at androidx.compose.ui.test.junit4.RobolectricIdlingStrategy$runUntilIdle$1.invoke(RobolectricIdlingStrategy.android.kt:51)
    at androidx.compose.ui.test.junit4.AndroidSynchronization_androidKt.runOnUiThread(AndroidSynchronization.android.kt:33)
    at androidx.compose.ui.test.junit4.RobolectricIdlingStrategy.runUntilIdle(RobolectricIdlingStrategy.android.kt:51)
    at androidx.compose.ui.test.junit4.AndroidComposeTestRule.waitForIdle(AndroidComposeTestRule.android.kt:293)
    at androidx.compose.ui.test.junit4.AndroidComposeTestRule.waitForIdle(AndroidComposeTestRule.android.kt:286)
    at androidx.compose.ui.test.junit4.AndroidComposeTestRule.setContent(AndroidComposeTestRule.android.kt:281)
Run Code Online (Sandbox Code Playgroud)

(堆栈跟踪中的下一行是我的测试代码。)

我在用:

  • 撰写1.0.0
  • 科特林 1.5.10
  • 浓缩咖啡3.4.0
  • 机器人电动4.6.1

我是在 AS 中还是在命令行中运行测试并不重要。

android robolectric android-testing android-jetpack-compose

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

测试 Room AutoMigrations 时,我应该向 runMigrationsAndValidate 传递什么?

使用Room AutoMigrations时,它Migration本身会自动生成。但为了对迁移进行单元测试,我必须将一个Migration对象传递给runMigrationsAndValidate. 我应该在这里传递什么?

@RunWith(AndroidJUnit4::class)
class MigrationTest {
    private val TEST_DB = "migration-test"

    @Rule
    val helper: MigrationTestHelper = MigrationTestHelper(
            InstrumentationRegistry.getInstrumentation(),
            MigrationDb::class.java.canonicalName,
            FrameworkSQLiteOpenHelperFactory()
    )

    @Test
    @Throws(IOException::class)
    fun migrate1To2() {
        var db = helper.createDatabase(TEST_DB, 1).apply {
            // db has schema version 1. insert some data using SQL queries.
            execSQL(...)

            // Prepare for the next version.
            close()
        }

        // Re-open the database with version 2 and provide
        // Migration as the migration process.
        db …
Run Code Online (Sandbox Code Playgroud)

android android-testing android-sqlite android-room

9
推荐指数
1
解决办法
1401
查看次数

收到“应用程序中未找到撰写视图。您的活动已恢复吗?”

我有一个Composable有一个TextButtonText将显示P当前方向是否为纵向,L否则。单击Button会将方向更改为横向,(因此之后,应该将文本从 更改PL

这是可组合的

@Composable
fun MyApp() {
    val currentOrientation = LocalConfiguration.current.orientation
    val orientation = if (currentOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            "P"
        } else {
            "L"
        }
    val activity = LocalContext.current as Activity
    Column {
        Text(text = orientation)
        Button(onClick = {
            // change orientation to landscape
            activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        }) {
            Text(text = "DO IT")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我测试的方法

@get:Rule
val composeRule = createComposeRule() …
Run Code Online (Sandbox Code Playgroud)

android android-testing android-instrumentation android-jetpack-compose

9
推荐指数
1
解决办法
4198
查看次数