android - 使用 UI 测试用例在 android studio 中运行测试

标签 android android-studio junit android-testing

我正在尝试在我当前工作的应用程序中添加 UI 测试用例。 为此,我在 app/src/androidtest/java/com.testapp

中添加了这个
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

/**
 * Instrumented test, which will execute on an Android device.
 *l
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {


    @Test
    fun loginActivityTest() {
// Type text and then press the button.
        onView(withId(R.id.editTextUserName)).perform(typeText(STRING_EMAIL),
                closeSoftKeyboard())
        onView(withId(R.id.editTextUserPassword)).perform(typeText(STRING_PASSWORD),
                closeSoftKeyboard())
        onView(withId(R.id.btnLoginSigin)).perform(click())

        // This view is in a different Activity, no need to tell Espresso.
//        onView(withId(R.id.show_text_view)).check(matches(withText(STRING_TO_BE_TYPED)))
    }

    companion object {
        val STRING_EMAIL = "test@test.com"
        val STRING_PASSWORD = "test1234"
    }
}

在 build.gradle 中添加:

// Testing-only dependencies
androidTestImplementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
androidTestImplementation 'androidx.test:core:' + rootProject.coreVersion
androidTestImplementation 'androidx.test:core-ktx:' + rootProject.coreVersion
androidTestImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
androidTestImplementation 'androidx.test.ext:junit-ktx:' + rootProject.extJUnitVersion
androidTestImplementation 'androidx.test:runner:' + rootProject.runnerVersion
androidTestImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion

testImplementation 'androidx.test:core:' + rootProject.coreVersion;
testImplementation 'androidx.test.ext:junit:' + rootProject.extJUnitVersion
testImplementation 'junit:junit:4.12'
testImplementation 'org.robolectric:robolectric:' + rootProject.robolectricVersion
testImplementation 'androidx.test.espresso:espresso-core:' + rootProject.espressoVersion
testImplementation 'androidx.test.espresso:espresso-intents:' + rootProject.espressoVersion
testImplementation 'androidx.test.ext:truth:' + rootProject.extTruthVersion


//Test
buildToolsVersion = "28.0.3"
androidxLibVersion = "1.0.0"
robolectricVersion = "4.3"
extTruthVersion = '1.3.0-alpha02'
coreVersion = "1.2.1-alpha02"
extJUnitVersion = "1.1.2-alpha02"
runnerVersion = "1.3.0-alpha02"
espressoVersion = "3.3.0-alpha02"

但是在编辑配置后只运行测试类。 我收到错误:

    Task :app:compileappnameqaDebugAndroidTestJavaWithJavac FAILED
F:\test_project\Android_new\app\build\generated\source\buildConfig\androidTest\debug\com\appname\test\BuildConfig.java:17: error: cannot find symbol
  public static final int appIcon = R.mipmap.ic_launcher;
                                     ^
  symbol:   variable mipmap
  location: class R
F:\test_project\Android_new\app\build\generated\source\buildConfig\androidTest\debug\test\com\appname\test\BuildConfig.java:23: error: cannot find symbol
  public static final int splashImage = R.drawable.splash;
                                         ^
  symbol:   variable drawable
  location: class R
2 errors

谁能帮我知道如何在我的应用中运行 UI 测试用例?

最佳答案

添加行后:

@get:Rule val activityRule = activityScenarioRule<LoginActivity>()

我仍然面临同样的问题。然后我复制了创建 res/drawable 和 res/mipmap 的可绘制文件和 mipmap 文件,并将这些文件粘贴到文件夹中。 结果消除了 BuildConfig 错误。然后我开始面临错误:

Command line is too long. Shorten command line for 'testconfiguration' or also for Android JUnit default Configuration

为了克服这个: 在测试配置设置中选择选项“类路径文件”,这将运行测试。

关于android - 使用 UI 测试用例在 android studio 中运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59063603/

相关文章:

Android 在滚动列表时禁用点击 ListView 项

android - Android2.1读取邮件附件?

java - Spring 数据 : Method with annotation @Query doesn't work as expected

java - JUnit 入门

java - 如何在 Junit 中使 `_logger.isDebugEnabled()` 条件为假?

android - 通过 ADB 将数据从 Android Activity 发送到 PC

java - 使用 SQLite 创建表时出错

android - 错误 : Gradle 'HelloWorld' project refresh failed: Build script error, 找到不支持的 Gradle DSL 方法 : 'setRoot()' !

android - 无法通知项目评估监听器 > javax/xml/bind/annotation/XmlSchema

java - 在获取预报之前更新位置