将自定义应用程序与 InstrumentationTestCase 结合使用 [英] Using a custom Application with InstrumentationTestCase

查看:44
本文介绍了将自定义应用程序与 InstrumentationTestCase 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ActivityInstrumentationTestCase2(它是 InstrumentationTestCase 的子类).运行我的测试用例时,我需要使用自定义 TestApplication 对象启动我的活动,因为这个 TestApplication 对象具有一些测试所需的配置.

I have an ActivityInstrumentationTestCase2 (which is a subclass of InstrumentationTestCase). When running my testcases, I need to launch my activities using a custom TestApplication object, since this TestApplication objects has some configuration necessary for my tests.

但是,我没有看到自定义 ActivityInstrumentationTestCase2 测试用例以使用测试 Application 对象的方法.有办法吗?

However, I don't see a way to customize my ActivityInstrumentationTestCase2 testcases to use the test Application object. Is there a way to do so?

推荐答案

我不知道是否有更好的方法,但我能够通过使用自定义 TestRunner 来完成此操作.

I don't know if there's a better way, but I was able to accomplish this by using a custom TestRunner.

public class MyInstrumentationTestRunner extends InstrumentationTestRunner {

    @Override
    public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        return new MyTestApplication(context);       
    }


}

我还需要修改我的测试项目的 AndroidManifest.xml 以指定新的运行程序:

I also needed to modify my test project's AndroidManifest.xml to specify the new runner:

<instrumentation android:name="com.mypackage.test.MyInstrumentationTestRunner" ... />

而且我不得不修改我的 IDE 以使用指定的测试运行程序.如果您从命令行运行,则需要执行以下操作:

And I had to modify my IDE to use the specified test runner as well. If you're running from the command line, you'll need to do something like the following instead:

adb shell am instrument -w com.mypackage/com.mypackage.test.MyInstrumentationTestRunner

这篇关于将自定义应用程序与 InstrumentationTestCase 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆