且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android Studio Unit Test中的Assets文件夹

更新时间:2022-06-13 01:10:32

看起来你正在尝试创建一个检测单元测试,因为你想要创建它位于androidTest文件夹中。

It looks like you're trying to create an instrumented unit test, since you want to create it in the androidTest folder.

您可以在测试中使用这两行中的一行来获取上下文:

You can use one of these two lines in your test to get the context:


  • 上下文ctx = InstrumentationRegistry.getTargetContext();
    这个将为您提供应用程序的上下文。您可以使用它来获取src / main / assets中的资产。

  • Context ctx = InstrumentationRegistry.getTargetContext(); this one will give you your app's context. You can use it to grab assets that are in src/main/assets for example.

上下文ctx = InstrumentationRegistry.getContext();
这个将为您提供测试应用程序的上下文。您可以使用它来获取src / androidTest / assets

Context ctx = InstrumentationRegistry.getContext(); this one will give you the test app's context. You can use it to grab assets that are in src/androidTest/assets

中的资产如果您想了解有关单元测试资产的更多信息,可以阅读帖子。在此github文件中你有一个例子。

If you want to know more about assets in unit testing you can read this post. In this github file you have an example.