3
我有這個處理圖像的項目。我用來執行大部分實際圖像處理的庫需要滿足在Android設備或仿真器上運行這些測試。我想提供一些它應該處理的測試圖像,問題是我不知道如何將這些文件包含在androidTest APK中。我可以通過上下文/資源提供圖像,但我寧願不污染我的項目資源。有關如何在儀器化單元測試中提供和使用文件的任何建議?如何在儀表單元測試中使用文件
我有這個處理圖像的項目。我用來執行大部分實際圖像處理的庫需要滿足在Android設備或仿真器上運行這些測試。我想提供一些它應該處理的測試圖像,問題是我不知道如何將這些文件包含在androidTest APK中。我可以通過上下文/資源提供圖像,但我寧願不污染我的項目資源。有關如何在儀器化單元測試中提供和使用文件的任何建議?如何在儀表單元測試中使用文件
你可以閱讀,在您的src/androidTest/assets
目錄下面的代碼資源文件:
Context testContext = InstrumentationRegistry.getInstrumentation().getContext();
InputStream testInput = testContext.getAssets().open("sometestfile.txt");
它使用測試的情況下,而不是在樁的應用是很重要的。
所以從測試資產目錄中讀取的圖像文件,你可以做這樣的事情:
public Bitmap getBitmapFromTestAssets(String fileName) {
Context testContext = InstrumentationRegistry.getInstrumentation().getContext();
AssetManager assetManager = testContext.getAssets();
InputStream testInput = assetManager.open(fileName);
Bitmap bitmap = BitmapFactory.decodeStream(testInput);
return bitmap;
}
非常感謝您! – Mathijs