简体   繁体   English

如何编写需要Android上下文的集成测试?

[英]How can one write an Integration Test that requires an Android Context?

For example, testing input/output to an image file. 例如,测试对图像文件的输入/输出。 To create the file, the following code is used (summary): 要创建文件,使用以下代码(摘要):

 File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
 File image = File.createTempFile("tempImage",".jpg",storageDir);
 mCurrentPhotoPath = image.getAbsolutePath();
 mCurrentPhotoURI = FileProvider.getUriForFile(this,"com.example.android.fileprovider",image);
 File photoFile = image;

When I try to write an integration test using this code to create a file, I have issues with the getExternalFilesDir and this (which is a context). 当我尝试使用此代码编写集成测试来创建文件时, getExternalFilesDirthis (存在上下文)出现问题。 How can I effectively test without having an activity or context to pass? 如何在没有活动或上下文通过的情况下有效进行测试? Can I generate one? 我可以产生一个吗?

在工具测试中, InstrumentationRegistry.getTargetContext()返回要测试的程序包的Context

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM