在我的仪器测试中,我想在横向和纵向模式下测试一些东西,所以我想在测试开始之前设置方向.有没有办法以编程方式设置设备或模拟器方向?
我知道这个setRequestedOrientation()方法,但这适用于某个活动,如果另一个活动开始,我必须记得再次调用它.我正在寻找的是一种"全局"设置方向的方法,以便每个新活动都以该方向自动启动.
更新:
该解决方案必须满足2个要求:1)它不会让我更改我的生产代码,2)它需要在CI环境中运行.
我一直在想如何正确地测试依赖于Android的日期或时间变化的功能.假设我有每个月的第一天需要处理的事件,这个处理的结果取决于当前的日期/时间.我错过了在Android测试框架中伪造某个日期/时间的可能性.我们该如何测试那种东西?
当然,可以通过不是直接从框架查询日期/时间而是从另外的实体查询日期/时间来实时抽象.可以在测试代码中为此实体设置假日期/时间.但是,Android测试框架中是否真的不支持这样的常见需求?
我只写了一个自定义的视图类,除其他事项外,允许开发者轻松地设置边界(如setBorderWidth,setBorderColor,setBorderWidthLeft,等).我通过覆盖onMeasure/来做到这一点onDraw,我想测试View正确绘制这些边框.
理想情况下,我想要一个比单元测试更高级别的东西:基本上我想强制执行,如果我设置边框,它们按预期绘制; 如果我不设置它们就不会被绘制; 如果我更改它们,则会绘制新边框并且不再显示旧边框.这让我知道我的观点正在高水平运作.
我考虑过的事情:
这些对我来说都不是很好,但我倾向于2).还有其他想法吗?
有没有办法以编程方式测试活动的保存和恢复状态代码?我的意思是这样做:
如何测试为保存/恢复活动的生命周期而构建的代码?但是以自动化的方式.
我已经测试了activity.recreate()几乎我正在搜索的方法,但事实上它并没有重置我的活动字段,就像我正在杀死进程一样.因此即使我没有在我的onCreate方法中实现恢复功能,我的测试也可以通过(因为我的字段没有变化......).
我目前正在玩Espresso v2,我想知道这是否可能通过玩InstrumentationRegistry.getInstrumentation()?
我的Android应用程序有一个服务,根据应用程序的运行次数等参数向用户发送通知.通知在不同情况下的不同时间发送.我想测试在所有不同情况下是否在正确的时间发送通知.android是否提供了这种测试方式?
我无法弄清楚为什么会出现这个错误.
这是完整的错误
Error:Execution failed for task
':app:transformClassesWithMultidexlistForDebugAndroidTest'.
> java.io.IOException: The output jar is empty. Did you specify the proper
'-keep' options?
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用测试支持库框架测试基本的未绑定服务:http: //developer.android.com/tools/testing-support-library/index.html
LocalService.java:
public class LocalService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
LocalServiceTest.java:
@RunWith(AndroidJUnit4.class)
public class LocalServiceTest {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
@Test
public void testWithUnboundService() throws TimeoutException {
Intent serviceIntent =
new Intent(InstrumentationRegistry.getTargetContext(), LocalService.class);
mServiceRule.startService(serviceIntent);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行测试时,我得到以下TimeoutException:
java.util.concurrent.TimeoutException: Waited for 5 SECONDS, but service was never connected
at android.support.test.rule.ServiceTestRule.waitOnLatch(ServiceTestRule.java:258)
at android.support.test.rule.ServiceTestRule.bindServiceAndWait(ServiceTestRule.java:207)
at android.support.test.rule.ServiceTestRule.startService(ServiceTestRule.java:137)
at com.example.android.testing.ServiceTestRuleSample.LocalServiceTest.testWithBoundService(LocalServiceTest.java:71)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at …Run Code Online (Sandbox Code Playgroud) 我想测试一个处理Bundles的方法.但是,我无法在测试环境中创建(非null)Bundle对象.
给出以下代码:
Bundle bundle = new Bundle();
bundle.putString("key", "value");
boolean containsKey = bundle.containsKey("key");
Run Code Online (Sandbox Code Playgroud)
containsKey是true如果代码在应用程序上下文中执行,但false如果在一个单元测试执行.
我无法弄清楚为什么会这样,以及如何为我的测试创建一个Bundle.
当我运行我的测试时,显示以下错误
No such manifest file: build\intermediates\bundles\debug\AndroidManifest.xml
java.lang.ClassCastException: android.app.Application cannot be cast to gyg.android.reviews.ReviewApplication
Run Code Online (Sandbox Code Playgroud)
以下是Gradle依赖项
compile group: 'org.mockito', name: 'mockito-all', version: '2.0.2-beta'
testCompile "org.robolectric:robolectric:3.3.2"
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'junit:junit:4.12'
Run Code Online (Sandbox Code Playgroud)
这就是我开始测试课程的方式
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class ReviewListPresenterTest {
Run Code Online (Sandbox Code Playgroud)
我正在使用Roboelectric 3.3.2与Android Studio 2.3.2快速帮助将受到高度赞赏!
android unit-testing robolectric android-testing android-studio
我正在尝试检查单元测试的覆盖范围,所有的测试都通过了,但是当我通过右键单击测试文件来使用“使用覆盖率运行测试”来运行测试时,它完成后,一切恢复为0%吗?
看起来它应该只能阅读https://developer.android.com/studio/test/index.html,并且找不到任何说明为什么它将为所有内容返回0%的信息吗?
我缺少一些隐藏的设置来使它正常工作吗?
android-testing ×10
android ×9
unit-testing ×2
android-date ×1
java ×1
junit ×1
junit4 ×1
robolectric ×1