在espresso测试中使用writeToTestStorage方法保存位图可以非常有用。这可以让我们将测试结果保存到设备的本地存储中以备后续使用。以下是使用Kotlin编写的示例代码:
@Test
fun testSaveBitmapToTestStorage() {
// Get a reference to the bitmap you want to save
val bitmap = BitmapFactory.decodeResource(
InstrumentationRegistry.getInstrumentation().context.resources,
R.drawable.test_bitmap
)
// Specify the file name and path
val fileName = "test_bitmap.png"
val directory = InstrumentationRegistry.getInstrumentation().targetContext.filesDir
// Save the bitmap to file using writeToTestStorage method
writeToTestStorage(bitmap, fileName, directory)
// Verify that the file exists in the specified directory
val file = File(directory.toString() + "/" + fileName)
assertTrue(file.exists())
}
该测试用例获取一个位图引用,指定文件名和路径,使用writeToTestStorage方法将位图写入本地存储,并验证该文件是否存在。使用以下导入语句导入writeToTestStorage方法:
import androidx.test.runner.screenshot.WriteToTestStorage.writeToTestStorage
这需要在build.gradle文件中添加以下依赖项:
androidTestImplementation 'androidx.test.espresso:espresso-core:'
上一篇:Android-error:Defaultconstructornotfoundfortype
下一篇:Android-Firebase ChangeValueEventListener在Android API 27(版本8.1或更低)上停止工作。