android - 访问测试应用程序资产

Jos*_*eph 8 xml android unit-testing assets suite

我在测试应用程序的assets目录中有一个XML文件.我想从我的测试类的套件方法访问此文件.

即,

public static TestSuite suite(){InputStream stream = //返回资产的一些代码}

知道我怎么能这样做吗?我尝试使用Resources.Resources.getSystem().getAssets()但没有运气:-(请帮忙.

约瑟夫,副总统,约瑟夫

Mik*_*ery 26

我的测试应用程序遇到了同样的问题.不幸的是,看起来测试用例类中的getContext()实际上返回了测试中的应用程序的上下文.为了获得测试应用程序本身的上下文,您必须继承InstrumentationTestCase并调用getInstrumentation().getContext()以获取测试应用程序的上下文.


Dam*_*ski 2

final AssetManager assetMgr = context.getResources().getAssets();

final InputStream fileIn = assetMgr.open("my_file.txt", AssetManager.ACCESS_STREAMING);
Run Code Online (Sandbox Code Playgroud)

我们还可以使用此方法链接xml 文件的文本。