【问题标题】:Configure testLogging for instrumented tests为检测测试配置 testLogging
【发布时间】:2022-02-06 21:31:16
【问题描述】:

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

build.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 配置之外,还输出跳过的测试,但不输出通过的测试。

Output:

> 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

【问题讨论】:

    标签: android gradle junit android-gradle-plugin android-junit


    【解决方案1】:

    很遗憾,这是不可能的,因为connected${Variant}AndroidTest 任务不继承自 Gradle 的 AbstractTestTask,因此 testOptions.unitTests 对 android 插桩测试没有影响。

    此时你运气不好,除非你以某种方式扩展 Android 的连接测试任务并实现你的自定义任务,以补充 Gradle 的 testLogging 扩展。

    您可以检查任务源here,这是实际记录发生的地方here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      • 2020-09-04
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 2013-10-19
      相关资源
      最近更新 更多