我的测试套件中的一个测试用例有问题。我想要做的只是单击一个按钮并等待新活动启动。
ActivityMonitor monitor = getInstrumentation().addMonitor(MyActivity.class.getName(), null, false);
TouchUtils.clickView(this, searchButton);
Activity a = getInstrumentation().waitForMonitorWithTimeout(monitor, 60);
assertNotNull("The MyActivity is null.", a);
Run Code Online (Sandbox Code Playgroud)
根据文档,此代码应等待最多 60 秒以使监视器被击中。如果它被击中,活动将被返回(否则为空)。
发生的情况是,测试时不时会通过,但大多数时候它会失败,因为a == null. 我正在观察测试用例,我可以清楚地看到它没有等待 60 秒。只有一点。通过一些打印输出,我还注意到监视器实际上被击中了,但返回的活动getLastActivity()仍然是null。
1)这怎么可能?
2)我可以做些什么来避免它?
测试android主屏幕小部件的最佳方法是什么?很难找到任何示例代码:/哪些框架支持测试?
我已经设法让我的Android项目转换到JUnit4,当然我想要做的主要原因是无法正常工作.如果有人在这里有想法,会喜欢任何帮助.
我试图解决的问题是,如果构建未指向登台服务器,我想自动跳过某些测试.我已经设置了一个BUILD_TYPE使用gradle注入基本URL的设置.
我assumeThat在我的设置中设置了一个子句,它可以正确识别构建何时不进行分段,但不是暂停和忽略其余的测试,而是抛出异常并失败.
这是我的实时API测试的基类 - 我已经注释了这一点@RunWith(AndroidJUnit4.class),因此在理论上这应该始终与JUnit4运行器一起运行:
package com.[my package].nonuitests.liveservertests;
import android.support.test.runner.AndroidJUnit4;
import com.[my package].nonuitests.BaseAndroidTest;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Tests against the live API. All tests descending from this class will
* be ignored if the BUILD_TYPE is not staging.
*/
@RunWith(AndroidJUnit4.class)
public class BaseLiveServerTests extends BaseAndroidTest {
private static final String STAGE = "staging";
/******************
* SETUP/TEARDOWN *
******************/
@Override
public void setUp() throws Exception {
super.setUp();
//TODO: Y U NO WORK?! …Run Code Online (Sandbox Code Playgroud) 大约一个月前,我在1.5小时后升级到Android Studio 2.0很长一段时间.我有一套23个测试,它们构建在Espresso之上,可以驱动我的应用程序.
通常情况下,我使用Genymotion Pro,但我一直试图让CI服务器运行,我想使用"新的和改进的"谷歌Android模拟器.
今天我终于开始尝试新的模拟器了.安装我的应用程序并玩它后,我感到惊喜.
当我通过Android Studio运行我的测试时,我的乐观情绪很快就消失了.以下是我遇到的一些问题:
请注意,Genymotion 不会出现上述任何情况.
一些额外的信息:
我尝试过的事情:
我为此感到困惑.还有其他人经历过同样的事情吗?无论如何我可以让谷歌Android模拟器以有用的方式运行测试吗?
谢谢!
android android-emulator android-testing android-espresso android-studio-2.0
我无法调试android Instrumentation测试用例(使用espresso和UiAutomator)
但早期的测试用例调试工作正常,但最近的更新它不再起作用了.它总是在实例化单元测试时受到攻击

我可以在命令窗口看到它正在尝试启动PACKAGE_NAME**.test**
adb shell am instrument -w -r -e debug true -e class PACKAGE_NAME.LanguageScreenTest#testSelectImportant PACKAGE_NAME**.test**/android.support.test.runner.AndroidJUnitRunner
Run Code Online (Sandbox Code Playgroud) android android-testing android-studio android-instrumentation
我正在开发一个带有MVP架构的Android应用程序,我已经能够测试Presenter和Model类,但现在我正在尝试测试View方法.例如,我有以下观点:
public interface SplashView extends BaseMVPView {
void initPresenter();
void navigateToHome();
void onError(ApiError apiError);
}
Run Code Online (Sandbox Code Playgroud)
这是由一个活动实现的.
public class SplashActivity extends BaseActivity implements SplashView {
// MVP Presenter
private SplashPresenter splashPresenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initPresenter();
}
@Override
public int getLayoutId() {
return R.layout.activity_splash;
}
@Override
public void destroy() {
if(splashPresenter != null) {
splashPresenter.onDestroy();
splashPresenter = null;
}
}
@Override
public void initPresenter() {
if(splashPresenter == null) {
splashPresenter = new SplashPresenterImpl(this, ApiClient.getService());
sync();
}
} …Run Code Online (Sandbox Code Playgroud) 在仪器测试期间,我的应用程序抛出以下错误.我的非测试版本中没有抛出此错误.
android.content.res.Resources$NotFoundException: String resource ID #0x7f030002
Run Code Online (Sandbox Code Playgroud)
我有两个gradle模块
:app (android-application) //applicationId: com.app
:library (android-library) //applicationId: com.library
//:app includes :library as a dependency
Run Code Online (Sandbox Code Playgroud)
有问题的资源在:library模块中定义并抛出.
在仪器下,我的:library模块调用:
context.getResources().getString(com.library.R.string.pref_key) - 这会引发异常如果我在检测期间调试断点并调用:
context.getResources().getString(com.app.R.string.pref_key) - 这个有效!但这是我的:library模块中的代码,它没有依赖性:app.我怎样才能解决这个问题?注 - 错误仅发生在检测测试期间,而不是正常构建期间.
更新:在检测中,调用getString(com.library.R.string.pref_key)从:app模块中的类调用时起作用,但是从:library模块中的类抛出异常.从字面上看,Resources当从:appvs 调用时,在同一个实例上调用完全相同的方法会有不同的行为:library.
android android-testing android-gradle-plugin android-instrumentation
这是我第一次使用Espresso。在我的设备上工作时,工作流程非常好,但是在使用仿真器(在CI上也是如此)时,每个基于espresso的测试都会失败,并显示相同的错误。
错误为,Error performing <action> on <view>例如:
android.support.test.espresso.PerformException: Error performing 'fast swipe' on view 'is <com.package.ScrollGestureLayout{16806b3 V.E..... ........ 0,0-1000,1000}>'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:84)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:81)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:52)
at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:312)
at android.support.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:167)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:110)
at com.package.ScrollGestureLayoutTest.testScroll(ScrollGestureLayoutTest.java:57)
at com.package.ScrollGestureLayoutTest.testScrollUp(ScrollGestureLayoutTest.java:85)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
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.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:433)
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$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
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 org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at …Run Code Online (Sandbox Code Playgroud) 我在Android 19及更低版本上使用JunitParams运行程序时遇到异常.
以下测试套件将重现该问题
@RunWith(JUnitParamsRunner.class)
public class SimpleTest {
@Test
@Parameters({ "false", "true" })
public void simpleParameterizedTest(boolean enabled) {
// Should execute and pass
}
}
Run Code Online (Sandbox Code Playgroud)
测试应该执行并通过.
没有运行测试,因为"没有找到测试"
我检查了日志,发现以下异常.
09-07 12:17:01.187 3338-3355/com.twilio.video.test W/dalvikvm: Exception Ljava/util/regex/PatternSyntaxException; thrown while initializing Ljunitparams/naming/MacroSubstitutionNamingStrategy;
09-07 12:17:01.187 3338-3355/com.twilio.video.test E/TestExecutor: Fatal exception when running tests
java.lang.ExceptionInInitializerError
at junitparams.internal.TestMethod$1.computeValue(TestMethod.java:40)
at junitparams.internal.TestMethod$1.computeValue(TestMethod.java:35)
at junitparams.internal.Memoizer.get(Memoizer.java:11)
at junitparams.internal.TestMethod.describe(TestMethod.java:116)
at junitparams.internal.ParameterisedTestClassRunner.describeParameterisedMethod(ParameterisedTestClassRunner.java:161)
at junitparams.JUnitParamsRunner.describeMethod(JUnitParamsRunner.java:502)
at junitparams.internal.ParametrizedTestMethodsFilter.filteredMethods(ParametrizedTestMethodsFilter.java:30)
at junitparams.JUnitParamsRunner.getListOfMethods(JUnitParamsRunner.java:498)
at junitparams.JUnitParamsRunner.getDescription(JUnitParamsRunner.java:487)
at org.junit.runners.Suite.describeChild(Suite.java:123)
at org.junit.runners.Suite.describeChild(Suite.java:27)
at org.junit.runners.ParentRunner.shouldRun(ParentRunner.java:434) …Run Code Online (Sandbox Code Playgroud) 目前,我正在一个使用环氧树脂的项目中工作,并且很难思考如何使用它创建UI /集成测试。在Epoxy github的存储库中,我仅获得有关测试RecyclerView的提示,但我认为这不是最好的方法。
你能给我一些提示吗?
谢谢 :)
android-testing ×10
android ×9
epoxy ×1
gui-testing ×1
java ×1
junit ×1
junit4 ×1
mocking ×1
mvp ×1
testing ×1