当浓缩咖啡失败时,设置构建失败

时间:2016-07-05 14:15:15

标签: android jenkins gradle automated-tests android-espresso

Gradle在单元测试失败时将构建设置为失败,但是当检测测试失败时设置BUILD SUCCESSFUL(在我的情况下为espresso)任何人都知道如果在espresso失败时强制构建失败?

Ops:需要这个与Jenkins集成,因此当单元测试和espresso测试失败并且不想使用Jenkins Text-finder插件时它必须失败。

3 个答案:

答案 0 :(得分:0)

为了让每个人都知道,我根据答案创建了一个解决方案:android-gradle-plugin 1.2.2: Flag ignoreFailures causes error in task connectedAndroidTest,我做了

project.gradle.taskGraph.whenReady {
    connectedAndroidTest[flavor]Debug {
        ignoreFailures = false
    }
    connectedAndroidTest[flavor2]Debug {
        ignoreFailures = false
    }
 }

并执行./gradlew connectedAndroidTest --continue

现在它为两种风格运行所有检测测试,如果其中任何一种都出现故障,那么构建失败也是如此。

答案 1 :(得分:0)

我找到了更干净的解决方案,在root build.gradle中使用:

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

bool isPrime(int a)
{
    int i, p = 0;
    if (a == 1)
        return false;
    else
    {
        for (i = a - 1; i > sqrt(a); i--)
            if (a % i == 0)
                p++;
        if (p != 0)
            return false;
        else
            return true;
    }
}

vector<int> show(int n)
{
    vector<int> v;
    int i, j;
    if (n == 1)
    {
        v.push_back(2);
        v.push_back(3);
        v.push_back(5);
        v.push_back(7);
    }
    else
    {
        show(n - 1);

        if (n != 1)
            for (i = 0; i < v.size(); i++)
            {
                for (j = 1; j <= 9; j += 2)
                    if (isPrime((v.at(i) * 10) + j))
                        v.at(i) = (v.at(i) * 10) + j;
            }
    }
    return v;
}

int main()
{
    int n, s = 0, i;

    cin >> n;
    show(n);
    for (i = 0; i < show(n).size(); i++)
        cout << show(n).at(i) << endl;
    system("pause");
    return 0;
}

答案 2 :(得分:0)

此解决方案对我有用。 将此代码添加到UI测试类:

// Use this TestRule to setup IdlingResource
@Rule
@JvmField
val mIdlingResourceTestRule = CustomIdlingResTestRule()

这是CustomIdlingResTestRule的代码:

import androidx.test.espresso.IdlingPolicies
import androidx.test.espresso.IdlingRegistry
import com.jakewharton.espresso.OkHttp3IdlingResource
import com.kasikorn.retail.mbanking.kplus.basemodule.AppModule
import com.kasikorn.retail.mbanking.kplus.util.OkHttpProvider
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
import java.util.concurrent.TimeUnit

/**
 * TestRule class help to setup IdlingResource
 */
class CustomIdlingResTestRule : TestRule {
    protected var mIdlingResource: CustomIdlingResource = CustomIdlingResource()

    val idlingResourceOkHttp: OkHttp3IdlingResource by lazy {
        OkHttp3IdlingResource.create(AppModule.APP_CONTEXT, OkHttpProvider.getOkHttpInstance())
    }

    override fun apply(base: Statement?, description: Description?): Statement {
        return object : Statement() {
            override fun evaluate() {
                IdlingPolicies.setMasterPolicyTimeout(60, TimeUnit.SECONDS)
                IdlingPolicies.setIdlingResourceTimeout(26, TimeUnit.SECONDS)
                IdlingRegistry.getInstance().register(idlingResourceOkHttp, mIdlingResource)
                //NOTE: use try cache to ignore UI Test Failures
                try {
                    base?.evaluate()
                } catch (e: Throwable) {
                    e.printStackTrace()
                }

                IdlingRegistry.getInstance().unregister(idlingResourceOkHttp, mIdlingResource)
            }

        }
    }
}

import android.app.Activity
import android.util.Log
import androidx.test.espresso.IdlingResource
import androidx.test.espresso.core.internal.deps.guava.collect.Iterables
import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry
import androidx.test.runner.lifecycle.Stage

class CustomIdlingResource() : IdlingResource {
    private var mIsIdle = false
    private var resourceCallback: IdlingResource.ResourceCallback? = null

    override fun getName(): String = this.javaClass.simpleName

    override fun isIdleNow(): Boolean {
        if (mIsIdle) return true
        val currentActivity = getCurrentActivity()
        Log.d("LOG", this.javaClass.simpleName + " | isIdleNow() | currentActivity: $currentActivity")
        if (currentActivity != null) {
            mIsIdle = true
            resourceCallback?.onTransitionToIdle()
        }
        return mIsIdle
    }

    override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback?) {
        this.resourceCallback = callback
    }

    private fun getCurrentActivity(): Activity? {
        val activity = arrayOfNulls<Activity>(1)
        val activities: Collection<Activity> = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED)
        activity[0] = Iterables.getOnlyElement(activities)
        return activity[0]
    }
}