Merge pull request #5684 from nextcloud/flakyTests

Add rule to rety failing tests
This commit is contained in:
Tobias Kaminsky 2020-03-24 09:47:35 +01:00 committed by GitHub
commit 84de3091de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 3 deletions

View file

@ -0,0 +1,65 @@
/*
*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2020 Tobias Kaminsky
* Copyright (C) 2020 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.nextcloud.client
import android.util.Log
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
/**
* C&p from https://stackoverflow.com/questions/45635833/how-can-i-use-flakytest-annotation-now on 18.03.2020
*/
class RetryTestRule(val retryCount: Int = 5) : TestRule {
private val TAG = RetryTestRule::class.java.simpleName
override fun apply(base: Statement, description: Description): Statement {
return statement(base, description)
}
private fun statement(base: Statement, description: Description): Statement {
return object : Statement() {
override fun evaluate() {
Log.e(TAG, "Evaluating ${description.methodName}")
var caughtThrowable: Throwable? = null
for (i in 0 until retryCount) {
try {
base.evaluate()
return
} catch (t: Throwable) {
caughtThrowable = t
Log.e(TAG, description.methodName + ": run " + (i + 1) + " failed")
}
}
Log.e(TAG, description.methodName + ": giving up after " + retryCount + " failures")
if (caughtThrowable != null)
throw caughtThrowable
}
}
}
}

View file

@ -11,6 +11,7 @@ import android.net.Uri;
import android.os.Bundle;
import com.facebook.testing.screenshot.Screenshot;
import com.nextcloud.client.RetryTestRule;
import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.account.UserAccountManagerImpl;
import com.owncloud.android.datamodel.FileDataStorageManager;
@ -29,7 +30,7 @@ import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.Rule;
import java.io.File;
import java.io.FileWriter;
@ -39,7 +40,6 @@ import java.util.Collection;
import androidx.test.espresso.contrib.DrawerActions;
import androidx.test.espresso.intent.rule.IntentsTestRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
import androidx.test.runner.lifecycle.Stage;
@ -54,8 +54,9 @@ import static org.junit.Assert.assertTrue;
* Common base for all integration tests
*/
@RunWith(AndroidJUnit4.class)
//@RunWith(AndroidJUnit4.class)
public abstract class AbstractIT {
@Rule public RetryTestRule retryTestRule = new RetryTestRule();
protected static OwnCloudClient client;
protected static Account account;