在层次结构中没有找到匹配的视图:JetPack Compose



我正在尝试在jetpack撰写中使用Espresso进行测试。每次我运行它,它给出这个错误。

androidx.test. express . nomatchingviewexception:没有视图在层次结构中找到匹配:android. widgets . textview和view.getText()的实例,无论是否转换为匹配:is "Count">

我MainActivity.kt

package com.example.testing
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.testing.ui.theme.TestingTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
TestingTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
Greeting()
}
}
}
}
}
@Composable
fun Greeting() {
var counter by remember{
mutableStateOf(0)
}
Column(
modifier = Modifier
.fillMaxSize()
.wrapContentSize(Alignment.Center)
) {
Text(text = stringResource(id = R.string.Count),
modifier = Modifier
.padding(8.dp)
.testTag(stringResource(id =  R.string.)))
Button(onClick = { counter++ }) {
Text(text = stringResource(id = R.string.Increment))
}
}
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
TestingTheme {
Greeting()
}
}

我test.kt

package com.example.testing

import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import com.microsoft.appcenter.espresso.ReportHelper;
import androidx.lifecycle.Lifecycle
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import com.microsoft.appcenter.espresso.Factory
import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
@LargeTest
class ExampleInstrumentedTest {
@Rule
val activityRule = ActivityScenarioRule(MainActivity::class.java)  //espresso
@Test
fun testClick() {
onView(withText("Count")).check(matches(isDisplayed()))
} 
}

我有计数在string.xml作为计数。我有模拟器启动和运行良好。它似乎无法检测到活动。

在@Test中,如果我跳过
.check(matches(isDisplayed()))

onView(withText("Count")).check(matches(isDisplayed()))

我也试过junit4

val composeTestRule = createAndroidComposeRule<MainActivity>()

它工作,但浓缩咖啡不起作用

您可以使用:

@get:Rule
val composeRule = createComposeRule()
@Test
fun testClick() {
composeRule.setContent {
TestingTheme {
Greeting()
}
}
composeRule.onNodeWithText("Count").assertIsDisplayed()
}

也可以使用:

@OptIn(ExperimentalFoundationApi::class, ExperimentalComposeUiApi::class)
@get:Rule
val composeTestRule = createAndroidComposeRule<MainActivity>()
@OptIn(ExperimentalFoundationApi::class, ExperimentalComposeUiApi::class)
@Test
fun testClick() {
composeTestRule.onNodeWithText("Count").assertIsDisplayed()
}

最新更新