从 AndroidTestCase 调用活动
我正在编写一个 android 测试用例,它需要对正在测试的 Activity 执行一个单独的 Activity(不是为了测试,而是为了访问 contentresolver,以便我可以更改一些电话设置)。
是否有可能从测试用例或以其他方式启动活动。
我知道用于测试活动的 AndroidTestCase 类,我在测试中使用它,但是我需要使用 ContentResolver 来更改电话设置,然后测试被测活动的反应,因此我需要另一个应用程序组件来更改这些设置。
注意:我释放了多个活动测试背后的复杂性(需要 ActivityManager),但我只想使用它的方法来更改设置,这样我什至可以在 onCreate 方法中拥有逻辑。
I am writing an android test case which requires the execution of a seperate Activity to the Activity being tested (not for the sake of testing but just to gain access to the contentresolver so I can change some telephony settings).
Is it at all possible to start an activity from a test case or in another manner.
I am aware of the AndroidTestCase class used to test activities, an I am using it in my tests, however I need to use a ContentResolver to change telephony settings and then test the reaction of the activity under test so I need another application component to change these settings.
Note: I release the complexity behind multiple activity testing (requiring an ActivityManager) but I only want to use it's method to alter the settings so I could even have the logic in the onCreate method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Android 提供了一个特殊的检测框架来测试 Activity。您必须使用此框架,因为活动具有复杂的生命周期,在此提供的框架之外无法调用。查看 Android 文档“开发”部分中的“测试”链接,了解活动测试 。如果这不能回答您的问题,您可以稍微改一下。
编辑
您确实应该扩展 ActivityUnitTestCase 来测试 Activity,而不是 AndroidTestCase。您可以获得更多特定于您需要测试的功能。如果扩展 ActivityUnitTestCase,则会有一个名为 launchActivity 的函数。它将启动您需要的活动并为您提供该活动的实例,以便您可以调用其方法,例如 set、get 和 finish。这应该可以完成您一次操作单个或多个活动所需的任何操作。
示例代码:
Android provides a special instrumentation framework for testing Activities. You must use this framework since Activities have a complex lifecycle that is un-invokable outside this provided framework. Look under the Testing link in the Developmentsection of the Android documentation for Activity Testing. If this doesn't answer your question, you might rephrase it a bit.
Edit
You should really be extending ActivityUnitTestCase to test an Activity, not AndroidTestCase. You get more functionality specific to what you need to test. If you extend ActivityUnitTestCase there is a function called launchActivity. It'll launch the activity you need and give you an instance of the activity so that you can call methods on it such as set, get, and finish. This should do anything you need for manipulating single and multiple activities at a time.
Example code:
AndroidTestCase 和 ActivityInstrumentationTestCase2 都提供了获取上下文的方法
AndroidTestCase:
ActivityInstrumentationTestCase2
您可以使用这些上下文来启动另一个活动,但是权限是从被测试的应用程序中采用的,所以在我的情况下,使用 contentresolver 我只有相同的权限来更改设置在被测试的应用程序中执行此操作。
就我而言,这不好,所以我必须创建一个具有自己的权限的单独应用程序和一个后台服务,然后我可以通过使用上下文启动意图来控制。
AndroidTestCase and ActivityInstrumentationTestCase2 both provide methods to get Context
AndroidTestCase:
ActivityInstrumentationTestCase2
You can use these contexts to launch another activity, however the permissions is adopted from the application under test, so in my case with the contentresolver I only have the same permission to alter settings I do in the application under test.
In my case this is no good so I had to create a seperate application with it's own permissions and a background service I was then able to control by launching intents using the context.