标签: android-testing

Espresso 测试失败:AssertionFailedWithCause:想要匹配 1 个意图。实际匹配 0 个意图

我是 Android Instrumentation 测试的新手。我想检查我的注册活动是否与 Espresso 正常工作。

我也尝试过有意图地运行它。似乎不起作用。

以下是我的代码:

package com.varun.project;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.intent.rule.IntentsTestRule;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.intent.Intents.intended;
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.junit.Assert.assertEquals;

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

    @Rule
    public IntentsTestRule<Register> ResgisterTestRule =
            new IntentsTestRule<>(Register.class, true, true);

    @Test …
Run Code Online (Sandbox Code Playgroud)

android android-intent android-testing android-espresso

5
推荐指数
1
解决办法
2491
查看次数

在 Android Studio 3.0 中运行覆盖率测试失败

我右键单击我的 LoginActivityTest 文件并选择Run LoginActivityTest,它运行并完成了所有测试。

现在我想在覆盖范围内运行它。然后,我右键单击 LoginActivityTest 文件并选择Run LoginActivityTest with Coverage,花了一些时间来构建它,然后输出以下内容:

---- IntelliJ IDEA coverage runner ---- 
sampling ...
include patterns:
com\.example\.ui\.login\..*
exclude patterns:
Process finished with exit code 1
Class not found: "com.example.ui.login.LoginActivityTest"Empty test suite.
Run Code Online (Sandbox Code Playgroud)

有人有同样的问题吗?我错过了什么吗?我怎样才能让它在测试覆盖范围内运行?

android android-testing test-coverage

5
推荐指数
0
解决办法
504
查看次数

如何在 Espresso 测试中重新启动 Android 应用程序(/应用程序状态)?

我正在测试登录页面 - 特别是“自动登录”复选框,以便用户登录一次后,将在重新打开应用程序时自动登录(默认情况下,用户应从头开始登录)。

我怎样才能模拟这种行为?重新启动应用程序是唯一的方法吗?我可以以某种方式将应用程序重置到初始屏幕(就像重新启动一样),但应该保留用户数据/cookie吗?

android-testing android-espresso android-instrumentation

5
推荐指数
1
解决办法
3556
查看次数

在Android中,使用Webdriverio,如何通过资源id选择元素?

我是 Appium 的新手,我想为我的 Android 应用程序创建一个测试,

使用设备监视器中的检查器,我找到了我的元素资源 ID:com.appPackage:id/categoryIconImageView。我像下面这样使用它。而且我不知道为什么它不能正常工作?

const wdio = require("webdriverio");

const opts = {
  port: 4723,
  desiredCapabilities: {
    platformName: "Android",
    deviceName: "emulator-5556 device",
    udid: "emulator-5554",
    platformVersion: "8.0.0",
    appPackage: "com.appPackage",
    appActivity: "com.appActivity",
    appWaitActivity: "com.appWaitActivity",
    noReset: "true",
  }
};

const client = wdio.remote(opts);

var s= client.init().element("#com.appPackage:id/categoryIconImageView").click().end();
Run Code Online (Sandbox Code Playgroud)

阿皮姆日志

[HTTP] --> POST /wd/hub/session/028abf5e-43a7-44f6-ab27-9d49e56bab8e/element
[HTTP] {"using":"-android uiautomator","value":"new UiSelector().resourceId('com.opensooq.OpenSooq:id/categoryIconImageView')"}
[debug] [MJSONWP] Calling AppiumDriver.findElement() with args: ["-android uiautomator","new UiSelector().resourceId('com.opensooq.OpenSooq:id/categoryIconImageView')","028abf5e-43a7-44f6-ab27-9d49e56bab8e"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] …
Run Code Online (Sandbox Code Playgroud)

android automated-tests android-testing appium webdriver-io

5
推荐指数
1
解决办法
3310
查看次数

如何使用 MockK 在 android 测试中模拟私有函数?

我似乎无法在 android 测试中模拟私有函数。我还使用全开放插件进行预 P 测试。在非 Android 测试中,它运行没有问题。我认为它也应该在 android 上工作,因为它在 MockK-android 上被标记。这是没有实现还是我遗漏了一些明显的东西?

androidTestImplementation "io.mockk:mockk-android:1.8.7"

@OpenForTesting
class A {
    fun publicFun() = privateFun()
    private fun privateFun() {}
    protected fun protectedFun() {}
}

@Test
fun privateFunctionMock() {
    val spy = spyk<A>()
    val mock = mockk<A>()
    val a = A()

    val functions = a::class.functions // size -> 6
    val spyFunctions = spy::class.functions // size -> 5
    val mockFunctions = mock::class.functions // size -> 5

    every { spy["privateFun"]() } returns Unit

    a.publicFun()
}
Run Code Online (Sandbox Code Playgroud)


失败并出现异常,因为缺少私有函数。
io.mockk.MockKException:找不到用于动态调用的函数 …

kotlin android-testing mockk

5
推荐指数
1
解决办法
8323
查看次数

即使添加“核心测试”依赖项后,也无法在我的“jUnit”测试用例中导入 InstantTaskExecutorRule - Android 测试

我正在为我的LoginViewModel. 我想setValue()在我的MutableLiveData.

为了避免android.os.Looper 中的方法 getMainLooper 未模拟异常,我尝试在test 文件夹Rule内的 ViewModel 文件中添加以下内容。

@Rule public InstantTaskExecutorRule mInstantTaskExecutorRule = new InstantTaskExecutorRule();

为此,我添加了以下依赖项:

androidTestImplementation 'android.arch.core:core-testing:1.1.1' as dependency.
Run Code Online (Sandbox Code Playgroud)

但是,我仍然无法InstantTaskExecutorRule在我的 LoginViewModelTest 文件中导入。可能是什么问题?

尽管它被导入到androidTest文件夹中,我们在其中编写集成或 UI 测试用例!但不在我们编写 jUnit 测试用例的测试文件夹中!

先感谢您。

请参考构建。梯度文件:

apply plugin: 'com.android.application'

    android {
    compileSdkVersion 28
    defaultConfig {
    applicationId "com.test.login"
    minSdkVersion 19
    targetSdkVersion 28
    versionCode 2
    versionName "1.1"
    multiDexEnabled true
    testInstrumentationRunner 
    "android.support.test.runner.AndroidJUnitRunner"
}

dataBinding {
    enabled = true
}

testOptions {
    unitTests.returnDefaultValues …
Run Code Online (Sandbox Code Playgroud)

unit-testing viewmodel android-testing android-mvvm mutablelivedata

5
推荐指数
1
解决办法
4012
查看次数

如何通过 Android 仪器测试在我的计算机上创建文件?

我想在我的笔记本电脑上(而不是在 Android 模拟器上)创建带有输出的文本文件。

我使用这段代码,但它对我不起作用:

@Test
fun useAppContext() {
    val appContext = InstrumentationRegistry.getInstrumentation().targetContext

    val str = "OUTPUT"
    val outputStreamWriter =
        OutputStreamWriter(appContext.openFileOutput("output.json", Context.MODE_PRIVATE))
    outputStreamWriter.write(str)
    outputStreamWriter.close()
}
Run Code Online (Sandbox Code Playgroud)

如何从 Android Instrumented 测试写入我的计算机上的文件?(我知道这是可能的)

io android android-testing

5
推荐指数
2
解决办法
895
查看次数

无法调试Android InstrumentedTest + ActivityScenario + Koin + Mocck

尝试在我的设备上调试 androidTests 时出现以下错误。出现的唯一错误如下,测试将永远挂起,直到我停止它。如果我运行它,测试就完成了,问题是当我尝试调试它时。我已经尝试过:

  1. 禁用即时运行
  2. 通过kill-server 和start-server 命令重新启动adb。
  3. 重新启动 android studio。
  4. 使缓存失效并重新启动。
  5. 不同的手机
  6. 不同的电脑

这是我在 logcat 中看到的错误:

W/ting.ventasplu: Current dex file has more than one class in it. Calling RetransformClasses on this class might fail if no transformations are applied to it!
A/ting.ventasplu:nstrumentation.cc:267] Check failed: m == frame.method_ (m=0xb0a66f30, frame.method_=0x6fac48ac) Expected void java.lang.Object.wait(long, int), Found void java.lang.Object.wait(long, int)
Run Code Online (Sandbox Code Playgroud)

这是InstrumentedTestCode:

@RunWith(AndroidJUnit4::class)
class LoginActivity2InstrumentedTest  {

    lateinit var scenario: ActivityScenario<LoginActivity>

    val viewModel: LoginViewModel = mockk(relaxed = true)

    val app: KoinTestVentasPlusApplication = ApplicationProvider.getApplicationContext()


    @After
    fun …
Run Code Online (Sandbox Code Playgroud)

android adb android-testing koin

5
推荐指数
0
解决办法
460
查看次数

Android:使用协程进行片状 ViewModel 单元测试

我有一个虚拟机,例如

class CityListViewModel(private val repository: Repository) : ViewModel() {
    @VisibleForTesting
    val allCities: LiveData<Resource<List<City>>> =
        liveData(context = viewModelScope.coroutineContext + Dispatchers.IO) {
            emit(Resource.Loading())
            emit(repository.getCities())
        }
}
Run Code Online (Sandbox Code Playgroud)

我的测试是:

@ExperimentalCoroutinesApi
class CityListViewModelTest {
    @get:Rule
    val rule = InstantTaskExecutorRule()
    @get:Rule
    val coroutineTestRule = CoroutinesTestRule()

    @Test
    fun `allCities should emit first loading and then a Resource#Success value`() =
        runBlockingTest {
            val fakeSuccessResource = Resource.Success(
                listOf(
                    City(
                        1,
                        "UK",
                        "London",
                        Coordinates(34.5, 56.2)
                    )
                )
            )
            val observer: Observer<Resource<List<City>>> = mock()
            val repositoryMock: Repository = mock()

            val sut …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-testing android-viewmodel kotlin-coroutines

5
推荐指数
1
解决办法
1391
查看次数

如何测试 Jetpack Compose?

我使用新的 Jetpack Compose,现在是时候测试 UI 了。有谁知道测试它的工具吗?
由于 Espresso 主要依赖于视图,我认为它没有帮助。

android android-testing android-espresso android-jetpack-compose

5
推荐指数
1
解决办法
5760
查看次数