我已经使用SQLiteOpenHelper某些类中的android.database包实现了对数据库的访问(使用模式DAO).
我使用a为这些类编写了一些junit测试,AndroidTestCase但这导致测试使用与应用程序相同的数据库.
我读过ProviderTestCase2或者RenamingDelegatingContext可以用来分别测试数据库.不幸的是,我找不到任何很好的教程/示例,展示了如何使用ProviderTestCase2/RenamingDelegatingContext测试数据库.
任何人都可以指点我或给我一些提示或分享一些代码进行数据库测试?!
Cheeerrrrsss!乔治
学习Android测试有哪些好方法?我对学习Android测试感兴趣,我实际上并没有做TDD,而是将测试和代码编写在一起.
我在http://developer.android.com/tools/testing/index.html 和"Android应用程序测试指南"一书中阅读了所有信息并了解了基本概念,但几乎没有任何信息或示例我可以找.
这本书和示例非常基础,并展示了如何使用2个EditText框测试一个非常简单的活动,
我需要测试更复杂的东西,如IntentService,AsyncTask,ResultReceiver等.我有兴趣以TDD或几乎TDD方式构建我的应用程序.
有什么办法我可以学习那些东西(书籍,博客,例子......)或Android测试是非常罕见的吗?
我已经在开发者控制台上传的APK文件到Alpha测试选项卡,并添加了谷歌组我的Alpha测试人员.当尝试输入生成的URL时,我得到页面未找到错误(404).
是否与我的应用仍处于草稿模式这一事实相关?
我正在学习使用浓缩咖啡的android仪器测试.我有一个有抽屉菜单的应用程序,有一个叫做关于的菜单.我正在测试点击该菜单项和活动内容.
testfunction:
@Test
public void testNavigationDrawerAboutMenu() {
onView(withId(R.id.drawer_layout))
.perform(DrawerActions.open()); //open drawer
onView(withText("About")).perform(click());
onView(withId(R.id.aboutsptemail)).check(matches(withText(R.string.screen_about_support_email)));
onView(withId(R.id.aboutcpright)).check(matches(isDisplayed()));
onView(withId(R.id.aboutprivacy)).check(matches(isDisplayed()));
onView(withId(R.id.abouttermsconditions)).check(matches(isDisplayed()));
onView(withId(R.id.aboutsptemail)).perform(click());
}
Run Code Online (Sandbox Code Playgroud)
现在最后一个textview中嵌入了weblink.因此,当您点击它时,它会自动在应用程序的Web视图中打开链接(www.support.com).我想测试这个功能.所以我试过这个:
intended(hasComponent(WebViewActivity.class.getName())); //check if webview called on supportEmail link click
Run Code Online (Sandbox Code Playgroud)
但是此错误跟踪测试失败:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.test.espresso.intent.OngoingStubbing android.support.test.espresso.intent.Intents.internalIntending(org.hamcrest.Matcher)' on a null object reference
at android.support.test.espresso.intent.Intents.intending(Intents.java:155)
at com.ScanBuy.SmartLabel.NavigationDrawerActivityTests.testNavigationDrawerAboutMenu(NavigationDrawerActivityTests.java:94)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at …Run Code Online (Sandbox Code Playgroud) 我使用API 24在Android Studio 2.1.2上创建了一个默认的空项目.在示例项目中,Google提供了折旧类ApplicationTestCase:
此类在API级别24中已弃用.请改用ActivityTestRule.应使用Android测试支持库编写新测试.
样品:
import android.app.Application;
import android.test.ApplicationTestCase;
/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:为什么现在不推荐使用Android测试用例?如何用ActivityTestRule替换ApplicationTestCase?
编辑:
我尝试使用Expresso,但在API 24(compileSdkVersion 24)上我有这个错误:
Error:Conflict with dependency 'com.android.support:appcompat-v7'. Resolved versions for app (24.0.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
Error:Conflict with dependency 'com.android.support:design'. Resolved versions for app (24.0.0) and test app (23.1.1) differ. See …Run Code Online (Sandbox Code Playgroud) 我使用的是Android Studio 0.2.4,但我认为这个问题一般都与IntelliJ有关.我的测试在src/instrumentTest/java下,并且都扩展了AndroidTestCase.当我运行所有测试时(例如右键单击源文件夹并单击"运行..."),测试在Android模拟器上作为Android测试运行得很好(如运行/调试配置中所示).
但是,如果我尝试以相同的方式运行单个测试方法或测试类(右键单击该方法并单击"运行..."),则测试将作为正常的JUnit测试运行,而不是在模拟器上运行,这当然会失败(堆栈跟踪).即使我尝试创建一个新的运行配置,我也看不到创建除JUnit配置之外的任何东西.
在IDE中,如何将测试方法或测试类作为Android测试运行?
Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/ResultPrinter
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)
Caused by: java.lang.ClassNotFoundException: junit.textui.ResultPrinter
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 3 more
Run Code Online (Sandbox Code Playgroud) 我知道Espresso可以按照UiAutomator的方式点击边界.(x和y坐标)我已经阅读了文档,但我似乎无法找到它.任何帮助表示赞赏.谢谢
编辑
我找到了这个链接,但没有示例如何使用它,我主要关心的是它UiController是如何使用它.
我正在尝试设置一个单元测试模块,如android studio博客文章中所述.但是,执行gradle构建失败告诉我"找不到名为'debug-classes'的配置".Debug是它试图构建的targetVariant的名称,但我不明白这里出了什么问题.
这是我的测试模块的gradle文件.
apply plugin: 'com.android.test'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
targetProjectPath ':app'
targetVariant 'debug'
}
Run Code Online (Sandbox Code Playgroud)
这是描述新测试模块功能的博客文章. http://android-developers.blogspot.com/2015/07/get-your-hands-on-android-studio-13.html
我正在使用Gradle插件v1.3.0
android android-testing android-studio android-gradle-plugin
如其他线程中所述,可以将Gradle配置为将测试结果记录到控制台中:
基本上,这可以通过以下任务设置:
tasks.withType(Test) {
testLogging {
// Custom configuration
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于单元测试,看起来有点像这样:
...
:app:assembleDebugUnitTest
:app:testDebugUnitTest
:app:processDebugResources
com.example.StringsTest > formatValue PASSED
com.example.StringsTest > formatValueWithDecimals FAILED
1 test completed, 1 failed
Run Code Online (Sandbox Code Playgroud)
此外,单元测试我也使用以下命令运行集成测试:
$ ./gradlew connectedAndroidTest
Run Code Online (Sandbox Code Playgroud)
当我查看控制台中的输出时,我错过了为单元测试编写的单个测试结果.如何为仪器测试配置测试日志记录?
我有一个使用Realm db的测验应用程序.每次用户选择答案时,她都会单击一个按钮,并显示问题的新文本.多数民众赞成,直到她到达我开始新活动的最后,并根据正确答案显示分数.
我应该如何开始/测试(使用Espresso我猜)该活动,而不必每次所有答案都手动输入并在每次回答后点击按钮,直到我到达最后一个?
我需要的是将一些模拟数据传递给变量并制作一个Intent,但我不知道如何在Espresso中找不到与此相关的任何内容