从 AndroidTestCase 调用活动

发布于 2024-10-27 13:31:56 字数 343 浏览 3 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

注定孤独终老 2024-11-03 13:31:56

Android 提供了一个特殊的检测框架来测试 Activity。您必须使用此框架,因为活动具有复杂的生命周期,在此提供的框架之外无法调用。查看 Android 文档“开发”部分中的“测试”链接,了解活动测试 。如果这不能回答您的问题,您可以稍微改一下。

编辑

您确实应该扩展 ActivityUnitTestCase 来测试 Activity,而不是 AndroidTestCase。您可以获得更多特定于您需要测试的功能。如果扩展 ActivityUnitTestCase,则会有一个名为 launchActivity 的函数。它将启动您需要的活动并为您提供该活动的实例,以便您可以调用其方法,例如 set、get 和 finish。这应该可以完成您一次操作单个或多个活动所需的任何操作。

示例代码:

@MediumTest
public class Test extends ActivityUniTestCase<HelloActivity> {

    public Test(Class<HelloActivity> activityClass) {
        super(activityClass);
    }

    @MediumTest
    public void testLifeCycleCreate() {
        HelloActivity hActivity = startActivity(new Intent(Intent.ACTION_MAIN), null, null);
        getInstrumentation().callActivityOnStart(hActivity);
        getInstrumentation().callActivityOnResume(hActivity);

        GoodByeActivity gActivity = launchActivity("package.goodbye", GoodByeActivity.class, null);
        gActivity.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:

@MediumTest
public class Test extends ActivityUniTestCase<HelloActivity> {

    public Test(Class<HelloActivity> activityClass) {
        super(activityClass);
    }

    @MediumTest
    public void testLifeCycleCreate() {
        HelloActivity hActivity = startActivity(new Intent(Intent.ACTION_MAIN), null, null);
        getInstrumentation().callActivityOnStart(hActivity);
        getInstrumentation().callActivityOnResume(hActivity);

        GoodByeActivity gActivity = launchActivity("package.goodbye", GoodByeActivity.class, null);
        gActivity.finish();
    }
}
习惯成性 2024-11-03 13:31:56

AndroidTestCase 和 ActivityInstrumentationTestCase2 都提供了获取上下文的方法

AndroidTestCase:

getContext();

ActivityInstrumentationTestCase2

getInstrumentation().getContext();

您可以使用这些上下文来启动另一个活动,但是权限是从被测试的应用程序中采用的,所以在我的情况下,使用 contentresolver 我只有相同的权限来更改设置在被测试的应用程序中执行此操作。

就我而言,这不好,所以我必须创建一个具有自己的权限的单独应用程序和一个后台服务,然后我可以通过使用上下文启动意图来控制。

AndroidTestCase and ActivityInstrumentationTestCase2 both provide methods to get Context

AndroidTestCase:

getContext();

ActivityInstrumentationTestCase2

getInstrumentation().getContext();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文