【单元测试】Android Testing 踩坑之旅

1. Espresso Toast 消息断言不适用于某些版本

  • 比如:android 11target sdk 30
  • 参考资料
  • https://github.com/android/android-test/issues/803

2. View 文案验证

  • 第一种方式
onView(withId(R.id.login_button)).check(matches(withText(getString(R.string.get_login_code))))
  • 第二种方式
val loginText = TextMatcher.getText(withId(R.id.login_button))
Truth.assertThat(loginText).matches(getString(R.string.get_login_code))

3. Fragment、DialogFragment

  • 报错如下
Unable to resolve activity for: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=cn.eeo.room.classin/androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity (has extras) }
at androidx.test.core.app.InstrumentationActivityInvoker.startActivity
  • 解决方案
// 不能使用:
androidTestImplementation 'androidx.fragment:fragment-testing:1.4.1'
// 必须使用:
debugImplementation 'androidx.fragment:fragment-testing:1.4.1'

4. perform(ViewActions.click()) 操作如果存在相同的 id 会导致无法正常运行

// R.id 唯一
onView(withId(R.id.xxx))
// R.id 不唯一
onView(allOf(withId(R.id.xxx), withText("Hello")))

// 虽然提供上述方式,但是在某些场景下还是无法执行

5. Mockk 框架 androidTestImplementation 导入两个出现错误

// gradle 导入如下:
def mockk_version = "1.12.4"
androidTestImplementation "io.mockk:mockk-android:$mockk_version"
androidTestImplementation "io.mockk:mockk-agent-jvm:$mockk_version"

// 报错如下:
Duplicate class io.mockk.ValueClassSupportKt found in modules 
jetified-mockk-agent-android-1.12.4-runtime (io.mockk:mockk-agent-android:1.12.4) 
and jetified-mockk-agent-jvm-1.12.4 (io.mockk:mockk-agent-jvm:1.12.4)
  • 解决方案
configurations {
  androidTestImplementation {
    exclude group: 'io.mockk', module: 'mockk-agent-jvm'
  }
}
  • 参考资料
  • https://github.com/mockk/mockk/issues/722

6. Mockk 框架 1.12.4 版本存在问题

// gradle 导入如下:
def mockk_version = "1.12.4"
androidTestImplementation "io.mockk:mockk-android:$mockk_version"
androidTestImplementation "io.mockk:mockk-agent-jvm:$mockk_version"

// 报错如下:
Caused by: io.mockk.proxy.MockKAgentException: MockK could not self-attach a jvmti agent to the current VM. 
This feature is required for inline mocking.This error occured due to an I/O error during the creation of 
this agent:java.io.IOException: Unable to dlopen libmockkjvmtiagent.so: dlopen failed: library "libmockkjvmtiagent.so" not found
  • 解决方案
def mockk_version = "1.12.3"
androidTestImplementation "io.mockk:mockk-android:$mockk_version"
androidTestImplementation "io.mockk:mockk-agent-jvm:$mockk_version"
  • 参考资料
  • https://github.com/mockk/mockk/issues/297

7. Espresso typeText 无法输入 Unicode 字符

  • 报错如下
java.lang.RuntimeException: Failed to get key events for string 这是中文这是中文 (i.e. current IME does not understand how to translate the string into key events). As a workaround, you can use replaceText action to set the text directly in the EditText field.
at androidx.test.espresso.base.UiControllerImpl.injectString(UiControllerImpl.java:333)
at androidx.test.espresso.action.TypeTextAction.perform(TypeTextAction.java:108)
at androidx.test.espresso.ViewInteraction$SingleExecutionViewAction.perform(ViewInteraction.java:356)
at androidx.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:248)
at androidx.test.espresso.ViewInteraction.access$100(ViewInteraction.java:63)
at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:153)
at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:150)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.Handler.handleCallback(Handler.java:966)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:293)
at android.app.ActivityThread.main(ActivityThread.java:9551)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:586)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1204)
  • 解决方案
  • 可以通过 ViewActions.replaceText() 替换 ViewActions.typeText() 方法,typeText() 方法不允许输入中文,但是 replaceText() 方法允许输入,但是 replaceText() 方法不会有按键上的触发事件。
  • 参考资料
  • https://stackoverflow.com/questions/35905451/cannot-enter-unicode-characters-in-espresso
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值