使用Espresso测试框架来检查一个TextView是否正确显示来自资源的格式化字符串。下面是一个示例代码:
@Test
public void testTextViewDisplaysFormattedString() {
// 从测试资源中获取格式化字符串
String expectedText = InstrumentationRegistry.getInstrumentation().getContext()
.getString(R.string.formatted_string, "John", "Doe");
// 找到要测试的TextView
onView(withId(R.id.myTextView)).check(matches(withText(expectedText)));
}
在上面的示例中,我们首先使用InstrumentationRegistry.getInstrumentation().getContext().getString()
方法从测试资源中获取格式化字符串。然后,我们使用onView()
方法找到要测试的TextView,使用withId(R.id.myTextView)
指定TextView的id。最后,我们使用check(matches(withText(expectedText)))
来验证TextView是否显示了正确的格式化字符串。
请确保将R.id.myTextView
替换为您的实际TextView的id,以及R.string.formatted_string
替换为您的实际格式化字符串的资源id。