android - 在单独的模块中进行 Espresso 测试

标签 android testing gradle ui-automation android-espresso

我有 android 应用程序,我想使用 Espresso 框架来创建测试自动化工具和测试。但是我不想在我的应用程序中创建一些东西,并且想要使用 Espresso 的单独模块来启动我的应用程序并使用 Espresso 对其进行测试。我使用安卓工作室。所以..你有什么想法如何解决这个问题吗?

最佳答案

您可以使用 library module specialised for instrumentation tests .基本上只是创建一个库模块,但使用不同的插件:

apply plugin: 'com.android.test' // A plugin used for test-only-modules 
您还需要更改您的android在您的测试模块中的 build.gradle 文件中加入一点:
android {
    [...]

    defaultConfig {
        [...]
        testApplicationId "com.yourcompany.yourapp.test"
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }

    [...]

    // Set the target app project.
    // The module specified here should contain the
    // production code test should run against.
    targetProjectPath ':app'
}
然后在测试模块的依赖项中添加您的应用程序和所有 espresso 库等:
dependencies {
    implementation project(':app')

    // All your test dependencies etc.
    implementation "androidx.test.espresso:espresso-core:$espresso_version"
}
使用 gradle 同步和运行测试,您可以在本地执行测试。如果您需要它进行外部测试,您还将获得该模块的所有正常 gradle 任务,您可以使用这些任务来组装您的测试 APK 等。在 Firebase 测试实验室中。

关于android - 在单独的模块中进行 Espresso 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31272191/

相关文章:

android - 如何强制 Activity 从头开始​​?

android - 为 Android 应用程序设计一组 "guiding screens"的最佳方法是什么?

android - 如何在命令行中为新的 Android 项目构建 gradle

android - Android Studio Gradle编译时间错误

android - 正确的 Android 游戏线程

java - 我如何与 Intent.ACTION_SEND 一起共享和保存图像和文本?

asp.net - 如何对 ASP.net 项目进行模糊测试

perl - Test::File::Find::Rule 检查我们所有的 perl 脚本是否使用严格

ruby - Cucumber 是否支持元功能?

intellij-idea - IntelliJ 下载 Gradle 依赖项但在编译期间找不到它们