gpt4 book ai didi

android - 测试失败后从 ActivityTestRule 重新启动 Activity

转载 作者:行者123 更新时间:2023-11-29 01:17:56 24 4
gpt4 key购买 nike

我修改了 ActivityTestRule 以重试执行失败的测试用例。每当测试失败时,我都需要重新启动 Activity 并重新运行执行。我已尝试调用 mActivity.finish() 但当前 Activity 未完成且未重新启动。但是测试重新开始。我从here开始遵循这个方法

public class MyActivityTestRule<T extends Activity> extends ActivityTestRule<T> {

private final Class<T> mActivityClass;
private T mActivity;
private static final String TAG = "ActivityInstrumentationRule";
private boolean mInitialTouchMode = false;
private Instrumentation mInstrumentation;


public MyActivityTestRule(Class<T> activityClass, boolean initialTouchMode, boolean launchActivity) {
super(activityClass, initialTouchMode, launchActivity);
mActivityClass = activityClass;
mInitialTouchMode = initialTouchMode;
mInstrumentation = InstrumentationRegistry.getInstrumentation();
}

@Override
public Statement apply(Statement base, Description description) {
final String testClassName = description.getClassName();
final String testMethodName = description.getMethodName();
final Context context = InstrumentationRegistry.getTargetContext();
/* android.support.test.espresso.Espresso.setFailureHandler(new FailureHandler() {
@Override public void handle(Throwable throwable, Matcher<View> matcher) {
if(AutomationTestCase.mEnableScreenshotOnFailure)
SpoonScreenshotOnFailure.perform("espresso_assertion_failed", testClassName, testMethodName);
new DefaultFailureHandler(context).handle(throwable, matcher);
}
});*/
// return super.apply(base, description);
return statement(base, description);
}

private Statement statement(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Throwable caughtThrowable = null;
//retry logic
for (int i = 0; i < 3; i++) {
try {
launchAppActivity(getActivityIntent());
base.evaluate();
return;
} catch (Throwable t) {
caughtThrowable = t;
System.err.println(description.getDisplayName() + ": run " + (i+1) + " failed");
finishActivity();
} finally {
finishActivity();
}
}
System.err.println(description.getDisplayName() + ": giving up after " + 3 + " failures");
throw caughtThrowable;
}
};
}

void finishActivity() {
if (mActivity != null) {
mActivity.finish();
mActivity = null;
}
}

public T launchAppActivity(@Nullable Intent startIntent) {
// set initial touch mode
mInstrumentation.setInTouchMode(mInitialTouchMode);

final String targetPackage = mInstrumentation.getTargetContext().getPackageName();
// inject custom intent, if provided
if (null == startIntent) {
startIntent = getActivityIntent();
if (null == startIntent) {
Log.w(TAG, "getActivityIntent() returned null using default: " +
"Intent(Intent.ACTION_MAIN)");
startIntent = new Intent(Intent.ACTION_MAIN);
}
}
startIntent.setClassName(targetPackage, mActivityClass.getName());
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.d(TAG, String.format("Launching activity %s",
mActivityClass.getName()));

beforeActivityLaunched();
// The following cast is correct because the activity we're creating is of the same type as
// the one passed in
mActivity = mActivityClass.cast(mInstrumentation.startActivitySync(startIntent));

mInstrumentation.waitForIdleSync();

afterActivityLaunched();
return mActivity;
}}

最佳答案

如果您的测试以不同于 ActivityTestRule 初始化的 Activity 结束,您将不得不关闭后台堆栈中的所有 Activity 。这个例子可能有帮助:

https://gist.github.com/patrickhammond/19e584b90d7aae20f8f4

关于android - 测试失败后从 ActivityTestRule 重新启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38473521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com