Gradle Test Logger Plugin

repository·develop·Indexed 21 days ago

https://github.com/radarsh/gradle-test-logger-plugin

A Gradle plugin that provides customizable, readable test logs in the console during execution. It features multiple visual themes (plain, standard, mocha) with parallel execution support, configurable exception and stack trace visibility, and slow test thresholds. Requires Java 17 and supports Gradle versions 4.x through 7.6+ depending on the plugin version. Configuration is available via the `testlogger` extension in Groovy or Kotlin DSL, and can be overridden at runtime using system properties.

Tokens
2.6K
Snippets
7
Records
16
Agent score
25%

What's inside gradle-test-logger-plugin

  1. Available visual themes for Gradle Test Logger Plugin

    develop

    The plugin supports several visual themes for displaying test results in the console. These themes can be categorized by their visual style (Standard, Mocha, Plain) and whether they support parallel execution output (Parallel variants).

    Standard Themes

    • Standard theme: The default visual style.
    • Mocha theme: A style inspired by Mocha test reports.
    • Plain theme: A minimal, unstyled output.

    Parallel Themes

    If you are running tests in parallel, you can use parallel-specific versions of the themes to ensure the output remains organized:

    • Standard parallel theme
    • Mocha parallel theme
    • Plain parallel theme
  2. How `testlogger` interacts with Gradle's `Test.testLogging`

    develop

    The testlogger extension attempts to react to equivalent properties in Gradle's native Test.testLogging extension. However, explicitly configured testlogger properties take precedence over Gradle's native settings.

    PropertyTest.testLogging valuetestlogger valueEffective value
    showStandardStreamstruenot configuredtrue
    showStandardStreamstruefalsefalse
    showStandardStreamsfalsetruetrue
    showExceptionstruenot configuredtrue
    showExceptionstruefalsefalse
    showExceptionsfalsetruetrue
    showStackTracestruenot configuredtrue
    showStackTracestruefalsefalse
    showStackTracesfalsetruetrue
    showFullStackTracestestLogging.exceptionFormat = FULLnot configuredtrue
    showFullStackTracestestLogging.exceptionFormat = SHORTnot configuredfalse
    showFullStackTracestestLogging.exceptionFormat = FULLfalsefalse
    showFullStackTracestestLogging.exceptionFormat = SHORTtruetrue
    showCausestruenot configuredtrue
    showCausestruefalsefalse
    showCausesfalsetruetrue
  3. Use parallel themes for parallel test execution

    develop

    The plugin supports parallel test execution (when maxParallelForks is greater than 1). To use this, you must switch to one of the specialized parallel themes. Note that parallel themes sacrifice test grouping to maintain readability in parallel environments.

    Available parallel themes:

    • plain-parallel
    • standard-parallel
    • mocha-parallel
  4. Override `testlogger` settings at runtime

    develop

    You can override any testlogger configuration setting at runtime using system properties. This is useful for CI/CD or local debugging without modifying build files.

    • Naming Convention: Use testlogger.<setting_name>.
    • Precedence: System property overrides are applied after combining task and project level settings, meaning they take precedence over everything else.
    • Scope: A system property applies the same setting to all tasks.

    Example: To override the theme to standard via the command line: ./gradlew test -Dtestlogger.theme=standard

    ./gradlew test -Dtestlogger.theme=standard
  5. Install the Gradle Test Logger Plugin

    develop

    To use the plugin, add it to your plugins block in your Gradle build script. Note that Java 17 is a pre-requisite for using this plugin.

    Depending on your version of Gradle, you should select the appropriate version of the plugin as defined in the compatibility matrix.

    plugins {
        id 'com.adarshr.test-logger' version '4.0.0'
    }
  6. Configure Windows support for Unicode symbols

    develop

    The standard and plain themes work on Windows out of the box. However, to see Unicode symbols when using the mocha theme, you must configure your environment to support UTF-8:

    1. Set or update the JAVA_OPTS environment variable with the system property -Dfile.encoding=UTF-8.
    2. Change your terminal code page to 65001 by executing chcp 65001 in your terminal.
    chcp 65001
    # And set JAVA_OPTS
    export JAVA_OPTS="-Dfile.encoding=UTF-8"
  7. Configure exception and stack trace visibility

    develop

    Control how much detail is shown when tests fail:

    • showExceptions: Set to false to hide failure locations and messages.
    • showStackTraces: Set to false to show only the exception message instead of the full stack trace.
    • showCauses: Set to false to hide the causes of the exception.
    • showFullStackTraces: Set to true to remove the default filtering and show the entire stack trace (similar to Gradle's FULL exception format).
  8. Configure `testlogger` using Kotlin DSL

    develop

    When using the Kotlin DSL, the syntax for the testlogger extension uses assignment (=) and specific type enums (e.g., ThemeType, LogLevel).

    If configuring within a subprojects block, you must use configure<TestLoggerExtension> to access the extension properties.

    testlogger {
        theme = ThemeType.STANDARD
        showExceptions = true
        showStackTraces = true
        showFullStackTraces = false
        showCauses = true
        slowThreshold = 2000
        showSummary = true
        showSimpleNames = false
        showPassed = true
        showSkipped = true
        showFailed = true
        showOnlySlow = false
        showStandardStreams = false
        showPassedStandardStreams = true
        showSkippedStandardStreams = true
        showFailedStandardStreams = true
        logLevel = LogLevel.LIFECYCLE
    }
    
    // In subprojects block:
    subprojects {
        apply {
            plugin("com.adarshr.test-logger")
        }
    
        configure<TestLoggerExtension> {
            theme = ThemeType.STANDARD
            showExceptions = true
            // ...
        }
    }
  9. Configure the `testlogger` extension

    develop

    The plugin provides a testlogger extension that can be configured at the project level or for specific Test tasks. Settings configured at the project level serve as defaults and can be overridden by task-level configurations.

    Default Configuration (Groovy DSL)

    testlogger {
        theme 'standard'
        showExceptions true
        showStackTraces true
        showFullStackTraces false
        showCauses true
        slowThreshold 2000
        showSummary true
        showSimpleNames false
        showPassed true
        showSkipped true
        showFailed true
        showOnlySlow false
        showStandardStreams false
        showPassedStandardStreams true
        showSkippedStandardStreams true
        showFailedStandardStreams true
        logLevel 'lifecycle'
    }
  10. Configure slow test threshold and logging

    develop

    The slowThreshold setting (default: 2000 ms) defines the duration above which a test is considered "slow" and its execution time is logged.

    • Warning/Error Styles: In color-supported themes, durations greater than half the threshold are displayed in a warning style, and durations exceeding the threshold are displayed in an error style.
    • Disable Logging: To completely disable time logging, set slowThreshold to a very large value.
    • Filter by Slowness: Use showOnlySlow true to only display tests that exceed the threshold.
  11. Configure standard stream and result filtering

    develop

    You can control the visibility of standard output/error and the visibility of specific test results.

    Standard Streams

    • showStandardStreams: Enables/disables display of stdout/stderr.
    • showPassedStandardStreams, showSkippedStandardStreams, showFailedStandardStreams: Filters stream output based on the test result type. These only have an effect if showStandardStreams is true.

    Test Result Filtering

    • showPassed, showSkipped, showFailed: Controls whether tests of that type are displayed in the log.
    • showOnlySlow: If true, only tests exceeding the slowThreshold are shown.
  12. Fix duplicate test output

    develop

    If you see test output appearing twice in your console, it is likely because Gradle's native testLogging is still enabled alongside this plugin.

    To fix this, remove any testLogging configuration blocks from your build.gradle file, such as:

      testLogging {
        events = ["FAILED"]
        exceptionFormat "full"
      }
    // REMOVE THIS from build.gradle to prevent duplicate output
    testLogging {
      events = ["FAILED"]
      exceptionFormat "full"
    }