如何从测试项目访问内部存储?

don*_*onV 8 testing android

我有一个使用测试数据的仪器测试.我已将此测试数据作为资产文件包含在测试项目中,并且运行良好.

我想通过将测试数据文件推送到测试应用程序的内部存储器来加速使用不同的测试数据重新运行测试,然后让测试从内部存储器读取测试数据(如果存在).

但是,我无法从测试应用程序访问内部存储.我正在使用的子类android.test.InstrumentationTestRunner,以及所有getContext().getFilesDir()返回的调用null.

测试应用程序无法访问内部存储吗?

任何帮助是极大的赞赏.

Ily*_*nko 1

如果您碰巧在测试中使用 Roboguice,请确保您实现应用程序类的构造函数,如下所示:

public MyApplication(Instrumentation instrumentation) {
    this();
    attachBaseContext(instrumentation.getTargetContext());

    //this does not work:
    //attachBaseContext(instrumentation.getContext());
}
Run Code Online (Sandbox Code Playgroud)