java espresso 自行车_java – 如何在Espresso中重新运行失败的测试? – 头脑风暴
我想弄清楚,如何使用Espresso重新運行失敗的測試.我認為從常見的JUnit測試案例來看,這有點復雜,因為您需要在測試開始之前恢復應用中的狀態.
我的方法是創建自己的ActivityTestRule,所以我只是從這個類中復制了整個代碼,并將其命名為MyActivityTestRule.
在儀器測試的情況下,規則還需要有關我們如何開始活動的信息.我更愿意自己推出它而不是為我做環境.例如:
@Rule
public MyActivityTestRule activityRule = new MyActivityTestRule<>(
ActivityToStartWith.class,true,false
);
所以我也在@Before注釋方法中啟動我的活動:
@Before
public void setUp() throws Exception {
activityRule.launchActivity(new Intent());
}
并在@After注釋方法中進行清理:
@After
public void tearDown() throws Exception {
cleanUpDataBaseAfterTest();
returnToStartingActivity(activityRule);
}
這些方法 – setUp(),tearDown()是每次測試運行之前/之后調用的必要條件 – 以確保測試開始期間的應用程序狀態是正確的.
在MyActivityTestRule中,到目前為止我做了一些修改.首先是應用方法的變更來自:
@Override
public Statement apply(final Statement base,Description description) {
return new ActivityStatement(super.apply(base,description));
}
這對我來說是個未知的事情,因為放在ActivityTestRule中的ActivityStatement具有super.apply方法,所以它還在UiThreadStatement中包裝了測試語句:
public class UiThreadStatement extends Statement {
private final Statement mBase;
private final boolean mRunOnUiThread;
public UiThreadStatement(Statement base,boolean runOnUiThread) {
mBase = base;
mRunOnUiThread = runOnUiThread;
}
@Override
public void evaluate() throws Throwable {
if (mRunOnUiThread) {
final AtomicReference exceptionRef = new AtomicReference<>();
getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
try {
mBase.evaluate();
} catch (Throwable throwable) {
exceptionRef.set(throwable);
}
}
});
Throwable throwable = exceptionRef.get();
if (throwable != null) {
throw throwable;
}
} else {
mBase.evaluate();
}
}
}
不管我對我的測試做什么我永遠不能創建case mRunOnUiThread布爾值為true.如果在我的測試用例中,將出現帶有注釋@UiThreadTest的測試 – 或者這就是我從代碼中理解的內容.它永遠不會發生,我不使用任何類似的東西所以我決定忽略這個UiThreadStatement并將MyActivityTestRule更改為:
@Override
public Statement apply(final Statement base,Description description) {
return new ActivityStatement(base);
}
我的測試用例沒有任何問題.感謝我留下的所有東西 – 包圍mBase.evaluate()的是:
private class ActivityStatement extends Statement {
private final Statement mBase;
public ActivityStatement(Statement base) {
mBase = base;
}
@Override
public void evaluate() throws Throwable {
try {
if (mLaunchActivity) {
mActivity = launchActivity(getActivityIntent());
}
mBase.evaluate();
} finally {
finishActivity();
afterActivityFinished();
}
}
}
通常,只有在ActivityTestRule的第3個參數中設置構造函數值true時才會調用launchActivity.但我自己在setUp()中啟動測試,所以它永遠不會發生.
根據我的理解,mBase.evaluate()在@Test注釋方法中運行我的代碼.它還會在throwable被拋出期間停止測試用例.這意味著我可以抓住它并重新啟動它 – 就像在那里提出的那樣:
How to Re-run failed JUnit tests immediately?
好吧,我做了類似的事情:
public class ActivityRetryStatement extends Statement {
private final Statement mBase;
private final int MAX_RUNS = 2;
public ActivityRetryStatement(Statement base) {
mBase = base;
}
@Override
public void evaluate() throws Throwable {
Throwable throwable = null;
for (int i = 0; i < MAX_RUNS; i++) {
try {
mBase.evaluate();
// if you reach this lane that means evaluate passed
// and you don't need to do the next run
break;
} catch (Throwable t) {
// save first throwable if occurred
if (throwable == null) {
throwable = t;
}
// my try that didn't work
launchActivity(testInitialIntent);
// I've returned test to starting screen but
// mBase.envaluate() didn't make it run again
// it would be cool now to:
// - invoke @After
// - finish current activity
// - invoke @Before again and start activity
// - mBase.evaluate() should restart @Test on activity started again by @Before
}
}
finishActivity();
afterActivityFinished();
// if 1st try fail but 2nd passes inform me still that there was error
if (throwable != null) {
throw throwable;
}
}
}
因此,catch塊中的那些注釋是我不知道該怎么做的部分.我嘗試在setUp()中使用的intent上執行launchActivity,以便第一次運行測試.但是mBase.evaluate()并沒有讓它做出反應(測試用例沒有再次發生) – 沒有發生任何事情它不會真的拯救我,我認為.我在@SetUp中缺少一些啟動,它沒有被再次調用.我真的想找到一種方法如何正確地重新啟動整個測試生命周期@Before @Test @After再次.也許有人從代碼中調用Instrumentation或TestRunner.
有關如何做到的任何想法?
總結
以上是生活随笔為你收集整理的java espresso 自行车_java – 如何在Espresso中重新运行失败的测试? – 头脑风暴的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中的me关键字_java中的vo
- 下一篇: java网格画线_java 网格输出的类