需要上下文的Android单元测试 [英] Android Unit Tests Requiring Context

查看:391
本文介绍了需要上下文的Android单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写我的第一个Android数据库后端,我正在努力单元测试我的数据库的创建。

I am writing my first Android database backend and I'm struggling to unit test the creation of my database.

目前我遇到的问题是获得一个有效的上传对象传递给我的SQLiteOpenHelper实现。有没有办法在扩展TestCase的类中获取一个Context对象?我想到的解决方案是实例化我的TestCase的设置方法中的一个活动,然后将该活动的上下文分配给一个字段变量,我的测试方法可以访问...但似乎应该有一个更容易的方法。

Currently the problem I am encountering is obtaining a valid Context object to pass to my implementation of SQLiteOpenHelper. Is there a way to get a Context object in a class extending TestCase? The solution I have thought of is to instantiate an Activity in the setup method of my TestCase and then assigning the Context of that Activity to a field variable which my test methods can access...but it seems like there should be an easier way.

感谢您的参与!

Macy

推荐答案

您可以尝试切换到 AndroidTestCase < a>。从查看文档,它似乎应该能够提供一个有效的上下文传递给SQLiteOpenHelper。

You might try switching to AndroidTestCase. From looking at the docs, it seems like it should be able to provide you with a valid Context to pass to SQLiteOpenHelper.

编辑:
请记住,您可能必须在Eclipse的Android测试项目中设置测试,因为测试将尝试在模拟器(或实际设备)上执行。

Keep in mind that you probably have to have your tests setup in an "Android Test Project" in Eclipse, since the tests will try to execute on the emulator (or real device).

这篇关于需要上下文的Android单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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