6

I am using JUnit 4 to write Android test cases (these tests are not using the Emulator and are running as native tests). Within my code I use SL4J for logging, however when I run the unit tests, I am not able to see any of the logging output. For example statements such as the following:

  private static final Logger logger = LoggerFactory.getLogger(AClass.class);
  logger.warn("log output not visible in unit test");

Any ideas on if it is possible to get access to the logger output in unit tests?

Regards,

user3521637
  • 1,622
  • 2
  • 18
  • 25
  • Hi Jared, I already have this in my gradle file and this logs the differents stages of the test executino and dumpts out the stderr for example when I get an exception in the test. However unfortunatley it does not do what I need to which is for it to log my actual log statements in my code under test. – user3521637 Nov 23 '15 at 00:51
  • You really can just use `System.out.println` or `System.err.println`. There is nothing wrong with using them, and they always work in any Java context. – IgorGanapolsky Sep 30 '16 at 14:26

2 Answers2

8

I have answered this before but I cannot find the link to the answer right now.

Here is the Android gradle plugin solution:

android {
  // ...

  testOptions.unitTests.all {
    testLogging {
      events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
    }
  }
}

Here is the solution I use for any Gradle test:

tasks.withType(Test) {
    testLogging {
        exceptionFormat 'full'
        showCauses true
        showExceptions true
        showStackTraces true
        showStandardStreams true
    }
}
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
  • 1
    Does this solution depend on Gradle version or other things? I tried multiple gradle version (latest as well) without any success. Std output is eaten up by gradle, output is the same with and without "showStandardStreams true" Any other prerequisites? – Habib Sep 06 '16 at 19:53
  • No. Can you post a question with your build.gradle? You should always use the latest version of Gradle if you can. – Jared Burrows Sep 06 '16 at 20:00
  • 1
    [This](http://stackoverflow.com/questions/28960558/how-i-can-display-log-files-system-out-println-in-android-test) suggests that it works only if you apply the java plugin, so you need you test functions in a separate project. – Habib Sep 06 '16 at 20:54
  • @Habib Well, of course. `tasks.withType(Test) {` is for the java plugin. If you are using the `android` DSL with the Android plugin, the java plugin is already included. – Jared Burrows Sep 06 '16 at 21:08
  • I see. Then it seems that for android tests we can only match using "tasks.withType(com.android.build.gradle.internal.tasks.AndroidTestTask)", but that does not have a testLogging function, so it wont work for android tests. Do I miss something? – Habib Sep 06 '16 at 21:19
  • @Habib `tasks.withType(Test) {` are for tests in `src/test` not `src/androidTest`. – Jared Burrows Sep 06 '16 at 21:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122782/discussion-between-habib-and-jared-burrows). – Habib Sep 06 '16 at 22:43
0

The updated Kotlin DSL version of @JaredBurrows answer

ref: 1

In the project build.gradle.kts

import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {...}
}

allprojects {
    repositories {
        google()
        mavenCentral()
        maven(url ="https://jitpack.io")
    }

    tasks.withType<Test>().configureEach {
        testLogging {
            events = setOf(
                TestLogEvent.STARTED,
                TestLogEvent.PASSED,
                TestLogEvent.FAILED,
                TestLogEvent.SKIPPED,
                TestLogEvent.STANDARD_OUT,
                TestLogEvent.STANDARD_ERROR
            )

            exceptionFormat = TestExceptionFormat.FULL
            showExceptions = true
            showCauses = true
            showStackTraces = true
        }

        ignoreFailures = false
    }
}
lasec0203
  • 2,422
  • 1
  • 21
  • 36