Утверждение текста View с использованием Espresso
успешно, но perform(click())
, кажется, не работает.
Мой тестовый код:
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28])
class MainActivityTestRobo2 {
@Before
fun setUp() {
}
@Test
fun `test11`() {
val scenario = launchFragmentInContainer<MainFragment>()
scenario.recreate()
onView(withId(R.id.button1)).check(matches(withText("Button1"))) // success
onView(withId(R.id.message)).check(matches(withText("MainFragment"))) // success
onView(withId(R.id.button1)).perform(click()) // maybe not worked!
onView(withId(R.id.message)).check(matches(withText("Button1"))) // fail
}
}
Поскольку это click()
должно было измениться message
текст Button1
с MainFragment
. Кстати, я написал тестовый код под test
, а не под androidTest
.
build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
viewBinding {
enabled = true
}
defaultConfig {
applicationId "com.example.tstester"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
compileOptions {
targetCompatibility = 1.8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
def fragment_version = "1.2.3"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'
testImplementation 'org.robolectric:robolectric:4.3.1'
testImplementation 'junit:junit:4.12'
testImplementation 'androidx.test.ext:junit:1.0.0'
testImplementation 'androidx.test:core:1.0.0'
testImplementation 'androidx.test.espresso:espresso-core:3.2.0'
debugImplementation "androidx.fragment:fragment-testing:$fragment_version"
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Сообщение об ошибке:
androidx .test.espresso.base.DefaultFailureHandler $ AssertionFailedWithCauseError: 'with text: is "Button1" "не соответствует выбранному представлению. Ожидается: с текстом: is "Button1". Получено: "TextView {id = 2131165295, res-name = сообщение, видимость = VISIBLE, ширина = 12, высота = 41, has-focus = false, has-focusable = false, has- window-focus = true, is-clickable = false, is-enabled = true, is-focus = false, is-focusable = false, is-layout-запрашивается = false, is-selected = false, layout-params = androidx. constraintlayout.widget. ConstraintLayout$LayoutParams@2673904a, tag = null, root -is-layout-Request = false, has-input-connection = false, x = 154.0, y = 191.0, text = MainFragment , input-type = 0, ime-target = false, has-links = false} "
androidx.test.espresso.ViewInteraction.postAsynchronouslyOnUiThread (ViewInteraction. java: 312) в androidx.test.espresso.ViewInteraction.check (ViewInteraction. java: 294) ... еще 17
Процесс завершен с кодом выхода 255