2

我想testLogging为我的仪器测试进行配置。但是,Gradle 似乎忽略了我在android.testOptions.unitTests.all.testLogging. 在那里,我已配置应记录所有通过和失败的测试,但不应记录跳过的测试。但是,Gradle 不会记录我通过的测试,但会记录我跳过的测试。

构建.gradle

plugins {
    id 'com.android.application'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId 'com.example.myapplication'
        minSdk 26
        targetSdk 31
        versionCode 1
        versionName '1.0'

        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    testOptions {
        unitTests {
            all {
                testLogging {
                    events = ["passed", "failed"]
                }
            }
        }
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    androidTestImplementation 'androidx.test:runner:1.4.0'
}

ExampleInstrumentedTest.java

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.Ignore;
import org.junit.Test;

public class ExampleInstrumentedTest {
    @Test
    public void passedTest() {
        assertEquals(2, 1 + 1);
    }

    @Test
    public void failedTest() {
        assertEquals(3, 1 + 1);
    }

    @Test
    @Ignore
    public void skippedTest() {
        fail("Should be skipped");
    }
}

不幸的是,Gradle 没有考虑我的测试日志配置。除了我的 Gradle 配置之外,还会输出跳过的测试,但不会输出通过的测试。

输出

> Task :app:connectedDebugAndroidTest
Starting 3 tests on test(AVD) - 12

com.example.myapplication.ExampleInstrumentedTest > skippedTest[test(AVD) - 12] SKIPPED 

com.example.myapplication.ExampleInstrumentedTest > failedTest[test(AVD) - 12] FAILED 
    java.lang.AssertionError: expected:<3> but was:<2>
    at org.junit.Assert.fail(Assert.java:88)
Tests on test(AVD) - 12 failed: There was 1 failure(s).

我在 GitHub 上托管了我完整的最小示例项目:pmwmedia/android-test-example

4

1 回答 1

0

不幸的是,这是不可能的,因为connected${Variant}AndroidTesttask 不是从 Gradle 继承的AbstractTestTask,因此testOptions.unitTests对 android 检测测试没有影响。

在这一点上你很不走运,除非你以某种方式扩展 Android 的连接测试任务并实现你的自定义任务来补充 Gradle 的testLogging扩展。

您可以在此处检查任务源,这是实际日志记录发生的地方

于 2022-02-15T09:59:47.790 回答