我对 Android Instrumented Tests 有疑问。未检测到测试。
我的依赖
dependencies {...
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation "androidx.test:core-ktx:1.3.0"
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestUtil 'androidx.test:orchestrator:1.4.0'
debugImplementation "androidx.fragment:fragment-testing:1.3.3"
}
然后默认配置
defaultConfig {
applicationId "com.example.forage"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments clearPackageData: 'true'
}
和测试选项
testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}
最后是我的测试样本
package com.example.forage
import androidx.test.core.app.launchActivity
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.replaceText
import androidx.test.espresso.assertion.ViewAssertions.doesNotExist
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
/**
* Test the UI features that involved persisted data.
*/
@RunWith(AndroidJUnit4::class)
class PersistenceInstrumentationTests {
@Before
fun test1() {
launchActivity<MainActivity>()
onView(withId(R.id.add_forageable_fab)).perform(click())
onView(withId(R.id.name_input)).perform(replaceText("Name"))
onView(withId(R.id.location_address_input)).perform(replaceText("Address"))
onView(withId(R.id.notes_input)).perform(replaceText("Notes"))
onView(withId(R.id.save_btn)).perform(click())
}
@Test
fun test_new_forageable_is_displayed_in_list() {
onView(withText("Name")).check(matches(isDisplayed()))
onView(withText("Address")).check(matches(isDisplayed()))
}
项目发布在 GitHub链接上。
PS也许我没有提到一些需要提问的来源,但我只是初学者,请说一下)