在 Android 中测试自定义 ContentProvider
我已经编写了我的内容提供程序,应该将对 SqlLite 数据库中的 2 个表的访问进行包装。现在我想为其编写一些测试用例,但我从未这样做过。在阅读了开发人员指南的部分后,我必须说我没有设法进行任何测试。
下面是到目前为止我的代码。这是测试项目中唯一与我的主项目相对应的类。当我在 Eclipse 中执行它时,模拟器正确启动,包已安装,但不运行测试:
测试运行失败:测试运行未完成。 预计 1 次测试,收到 0 次
这是测试类:
public class ArticleProviderTest extends ProviderTestCase2<ArticleProvider> {
static final Uri[] validUris = new Uri[] { Articles.CONTENT_URI,
Pictures.CONTENT_URI,
Pictures.getContentUriForArticleId(1) };
public ArticleProviderTest(Class<ArticleProvider> providerClass, String providerAuthority) {
super(providerClass, providerAuthority);
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
public void testQuery() {
ContentProvider provider = getProvider();
for (Uri uri : validUris) {
Cursor cursor = provider.query(uri, null, null, null, null);
assertNotNull(cursor);
}
}
}
以及清单文件,如果有帮助的话:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fr.marvinlabs.xxxx"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<instrumentation android:targetPackage="fr.marvinlabs.xxxx" android:name="android.test.InstrumentationTestRunner" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="android.test.runner" />
</application>
</manifest>
当我在调试配置中启动时,构造函数和设置中的断点不会被触发。 ?!
我在网上也没有找到太多信息。有人可以帮助我了解如何设置测试(基本上创建一个测试数据库文件,用一些数据填充它,查询它,...)?
I have written my content provider supposed to wrap access to 2 tables in a SqlLite database. Now I'd like to write some test cases for it but I have never done it. After reading the section on the developer guide, I must say that I did not manage to get anything tested.
Below is my code so far. This is the only class in the test project that corresponds to my main project. When I execute it in Eclipse, the emulator starts correctly, the packages get installed but it does not run the test:
Test run failed: Test run incomplete.
Expected 1 tests, received 0
Here is the test class:
public class ArticleProviderTest extends ProviderTestCase2<ArticleProvider> {
static final Uri[] validUris = new Uri[] { Articles.CONTENT_URI,
Pictures.CONTENT_URI,
Pictures.getContentUriForArticleId(1) };
public ArticleProviderTest(Class<ArticleProvider> providerClass, String providerAuthority) {
super(providerClass, providerAuthority);
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
public void testQuery() {
ContentProvider provider = getProvider();
for (Uri uri : validUris) {
Cursor cursor = provider.query(uri, null, null, null, null);
assertNotNull(cursor);
}
}
}
And the manifest file, if it helps:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fr.marvinlabs.xxxx"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<instrumentation android:targetPackage="fr.marvinlabs.xxxx" android:name="android.test.InstrumentationTestRunner" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="android.test.runner" />
</application>
</manifest>
When I launch in debug configuration, breakpoints in the constructor and in the setUp don't get triggered. ?!
I also did not find much info on the net. Could anybody help me get some understanding on how the testing should be setup (basically create a test database file, fill it with some data, query it, ...)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,明白了。错误是我没有为测试类提供默认构造函数。我重写了错误的构造函数:
现在凌晨
2 点是你无法完全阅读文档的时间,下午更好:)
Ok, got it. Mistake was that I was not providing the default constructor for the test class. I had overridden the wrong constructor:
is now
2am is the time when you cannot read the docs entirely well, afternoon is better :)
我发现 SDK 提供的 NotePad 示例项目中的
NotePadProviderTest.java
是一个很好的开始。I found
NotePadProviderTest.java
in the NotePad sample project provided by the SDK to be a good start.您应该实现
setUp()
和tearDown()
方法来创建和删除数据库。这是一个很好的例子: http://www.google.com/codesearch/p?hl=en#IrmxZtZAa8k/tests/src/com/android/providers/calendar/CalendarProvider2Test.java
You should implement
setUp()
andtearDown()
methods in which you create and delete the database.This is a great example: http://www.google.com/codesearch/p?hl=en#IrmxZtZAa8k/tests/src/com/android/providers/calendar/CalendarProvider2Test.java