gpt4 book ai didi

android - IsolatedContext 与 AndroidTestCase.getContext()

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:41:11 25 4
gpt4 key购买 nike

我正在编写一些测试来测试我的 sqllite 数据库代码。这里有人可以解释一下,使用我从 AndroidTestCase.getContext() 获得的上下文来编写这些测试是否会有所不同?或使用 IsolatedContext .

最佳答案

对于那些不想点击 Google Group 链接的人,这里是那里给出的答案:

AndroidTestCase.getContext() returns a normal Context object. It's the Context of the test case, not the component under test.

IsolatedContext returns a "mock" Context. I put "mock" in quotes because its not a mock in the normal sense of that term (for testing). Instead, it's a template Context that you have to set up yourself. It "isolates" you from the running Android system, so that your Context or your test doesn't accidentally get outside of the test fixture. For example, an IsolatedContext won't accidentally hit a production database (unless you set it up to do that!) Note, however, that some of the methods in an IsolatedContext may throw exceptions. IsolatedContext is documented in the Developer Guide under Framework Topics > Testing, both in Testing Fundamentals and in Content Provider Testing.

Here is the Android docs on IsolatedContext.

And here is the relevant section of the Testing Fundamentals document.

关于android - IsolatedContext 与 AndroidTestCase.getContext(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4645461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com