I did search a lot but unfortunately couldn't get it to work.
Based on my search I found that I need to add following code into build.gradle file. However, Gradle seems doesn't recognize it and always says Geadle DSL method not found: test()
test {
testLogging.showStandardStreams = true
testLogging.events("passed", "skipped", "failed", "standardOut", "standardError")
afterTest { desc, result ->
println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
}
}
Update
I can confirm above code or better than that following code is working fine if you create a test project and move all your test cases inside that instead of src/test/java and src/androidTest/java in main project. It's because you can apply java plugin in build.gradle file. However, It's not possible to use following code in any build.gradle file that com.android.* has been defined. Since these two libraries are not compatible :(
apply plugin: 'java'
evaluationDependsOn(':YOUR-LIB')
test {
testLogging.showStandardStreams = true
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
exceptionFormat = 'full'
}
afterTest { desc, result ->
println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
}
forkEvery = 5
maxParallelForks = java.lang.Runtime.runtime.availableProcessors() / 2
}
tasks.withType(Test) {
// Need Gradle to ignore classes that are inner class of Test classes but not actually Tests
scanForTestClasses = false
include "**/*Test.class"
}
So, my question is does anyone's INVENTED any way to print out logs under android plugin?
Related
Gradle has the test configuration block
https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html
```
apply plugin: 'java' // adds 'test' task
test {
// enable TestNG support (default is JUnit)
useTestNG()
// set a system property for the test JVM(s)
systemProperty 'some.prop', 'value'
// explicitly include or exclude tests
include 'org/foo/**'
exclude 'org/boo/**'
// show standard out and standard error of the test JVM(s) on the console
testLogging.showStandardStreams = true
// set heap size for the test JVM(s)
minHeapSize = "128m"
maxHeapSize = "512m"
// set JVM arguments for the test JVM(s)
jvmArgs '-XX:MaxPermSize=256m'
// listen to events in the test execution lifecycle
beforeTest { descriptor ->
logger.lifecycle("Running test: " + descriptor)
}
// listen to standard out and standard error of the test JVM(s)
onOutput { descriptor, event ->
logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message )
}
}
where one can set all sorts of test configuration (I am mostly interested in the heap size). Is there something similar for android projects?
There is a possibility to add them. Android Gradle plugin has parameter testOptions, which has parameter unitTests, which has option all.
So if you write:
android {
testOptions {
unitTests.all {
// apply test parameters
}
}
}
the tests will be executed with specified parameters.
My company uses Buildkite for our CI services. When the CI server triggers a build, all logs from the build are sent to Buildkite and made available through the UI. This is useful since it allows us to check how a build failed.
Buildkite has a feature which creates collapsible groups from logs formatted like:
--- Compiling
logs after this point will be in a collapsible group named 'Compiling'
--- Testing
logs after this point will be in a collapsible group named 'Testing'
How can I add custom logging to a gradle build which will output these 'groups'? I'd like to have a group for compiling/assembling, one for running unit tests, etc.
I've considered adding tasks which perform the logging and making them dependencies of the built-in tasks, but I'm not sure how to do that, or if it's a good idea in general.
I'm using Gradle 2.12 and whipped up an example from the Logging doc. The example does not use Android Studio nor BuildKite, but I believe this will help with the fundamental question.
Given a simple build.gradle file for a typical Java project:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.11'
}
compileJava << { println "TRACER example log from compileJava" }
compileTestJava << { println "TRACER example log from compileTestJava" }
test << { println "TRACER example log from test" }
and an init.gradle file:
useLogger(new CustomEventLogger())
class CustomEventLogger extends BuildAdapter implements TaskExecutionListener {
public void beforeExecute(Task task) {
if (task.name ==~ "compileJava") {
println "--- Compiling"
} else if (task.name == "test") {
println "--- Testing"
}
}
public void afterExecute(Task task, TaskState state) {}
public void buildFinished(BuildResult result) {}
}
then this command-line:
$ gradle -I init.gradle test
yields this output:
--- Compiling
TRACER example log from compileJava
TRACER example log from compileTestJava
--- Testing
TRACER example log from test
When I execute Android tests on the command-line using Gradle, I would like to see test log output. I followed the suggestion in this post but events are not being logged.
My app/build.gradle:
apply plugin: 'com.android.application'
...
//Test Logging
tasks.withType(Test) {
testLogging {
events "started", "passed", "skipped", "failed"
}
}
My project has multiple product flavors (free, paid) and build types (debug, release). I expected this would configure these tasks for test logging:
connectedAndroidTestPaidDebug
connectedAndroidTestFreeDebug
connectedAndroidTestPaidRelease
...
In Android Studio, I see warnings:
Cannot resolve symbol 'testLogging'
Cannot resolve symbol 'events'
have you tried using :
testOptions.unitTests.all {
testLogging {
events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
}
}
I am running android tests using the Gradle Android plugin and want to see individual test results.
From answers to this Question Gradle: How to Display Test Results in the Console in Real Time? it seems I can either use --info (which prints a LOT of other verbose junk I don't care about) or use this closure which only works for the Java plugin (not the Android plugin)
test {
afterTest { desc, result ->
println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
}
}
Is there some other option / closure I can use when I am running the connectedCheck task just to print the individual test results without all the other "verbosity".
Use Gradle info
This will print all information from Gradle:
gradle --info
or Use Android Gradle plugin:
android.testOptions.unitTests.all {
// Configure whether failing tests should fail the build
ignoreFailures false
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
}
}
or Use Gradle directly:
allprojects {
tasks.withType(Test) {
testLogging {
exceptionFormat "full"
showCauses true
showExceptions true
showStackTraces true
showStandardStreams true
events = ["passed", "skipped", "failed", "standardOut", "standardError"]
}
}
}
See: https://github.com/jaredsburrows/android-gradle-java-app-template/blob/master/gradle/compile.gradle#L20
Output:
io.github.hidroh.materialistic.data.SessionManagerTest > testView PASSED
io.github.hidroh.materialistic.data.SessionManagerTest > testIsViewFalse PASSED
io.github.hidroh.materialistic.data.SessionManagerTest > testIsViewNull PASSED
io.github.hidroh.materialistic.data.SessionManagerTest > testIsViewTrue PASSED
io.github.hidroh.materialistic.data.SessionManagerTest > testViewNoId PASSED
Source: https://github.com/hidroh/materialistic/blob/master/robolectric.gradle
Gradle Docs: https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/logging/TestLogEvent.html
For me all the options mentioned in the other answer still printed a lot of verbose information. So despite the requirement that info should not be used, I successfully use the following. For example, if your tests are in the package com.example.android, you can use:
gradle --info connectedDebugAndroidTest | grep "com\.example\.android\..* >"
Will print e. g.:
com.example.android.login.LoginActivityTest > enterCredentialsTest[Testing_emulator(AVD) - 9] SUCCESS
And the word "SUCCESS" will be green, which is awesome.
For Android Studio (tested on com.android.tools.build:gradle:2.1.0 and gradle version gradle-2.10) I added the following section to print exceptions in full format as well as logging every executed test:
apply plugin: 'com.android.application'
android { ... }
dependencies { ...}
tasks.withType(Test) {
testLogging {
exceptionFormat "full"
}
afterTest { desc, result ->
println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
}
}
We have an Android application that we are building with Gradle/Android Studio and are using JaCoCo to generate code coverage reports for our unit tests; this is working great. We are also interested in being able to generate coverage reports for manual tests as well; that is, show what code was covered in an arbitrary application launch. It appears that JaCoCo's predecessor EclEmma was capable of this, but I have not been able to find any confirmation one way or the other about JaCoCo (though I am beginning to assume it impossible from the lack of discourse).
I have tried using EclEmma from Eclipse just to have something, but the latest version fails with this error, and I couldn't immediately get older versions to work either.
Can anyone confirm whether or not it is possible to generate coverage data on an arbitrary application launch with JaCoCo? As in, run the app, press buttons, close the app and get a report on what code was exercised by the buttons you pushed. If not, is there another tool that can accomplish this?
Thanks!
apply plugin: 'jacoco'
def coverageSourceDirs = [
'../app/src/main/java'
]
jacoco{
toolVersion = "0.7.4.201502262128"
}
task jacocoTestReport(type: JacocoReport) {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
reports {
xml.enabled = true
html.enabled = true
}
classDirectories = fileTree("enter code here"
dir: './build/intermediates/classes/debug',
excludes: ['**/R*.class',
'**/*$InjectAdapter.class',
'**/*$ModuleAdapter.class',
'**/*$ViewInjector*.class'
])
sourceDirectories = files(coverageSourceDirs)
executionData = files("$buildDir/outputs/code-coverage/connected/coverage.exec")
doFirst {
new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
if (file.name.contains('$$')) {
file.renameTo(file.path.replace('$$', '$'))
}
}
}
}
// this is for the report
debug {
testCoverageEnabled true
}
// this is for offline
add these to the build.gradle file.
add directory "resources" to the app>src>main
add jacoco-agent.properties file to resources.
write destfile=/sdcard/coverage.exec to file jacoco-agent.properties
now add this class to your project .
public class jacoco {
static void generateCoverageReport() {
String TAG = "jacoco";
// use reflection to call emma dump coverage method, to avoid
// always statically compiling against emma jar
String coverageFilePath = "/sdcard/coverage.exec";
java.io.File coverageFile = new java.io.File(coverageFilePath);
try {
Class<?> emmaRTClass = Class.forName("com.vladium.emma.rt.RT");
Method dumpCoverageMethod = emmaRTClass.getMethod("dumpCoverageData",
coverageFile.getClass(), boolean.class, boolean.class);
dumpCoverageMethod.invoke(null, coverageFile, false, false);
Log.e(TAG, "generateCoverageReport: ok");
} catch (Exception e) {
new Throwable("Is emma jar on classpath?", e);
}
}
}
when your app is onDestroy call the function
jacoco.generateCoverageReport();
you can run your app . when test over you can use command
"adb pull /sdcard/coverage.exec yourapp/build/outputs/code-coverage/connected/coverage.exec".
the last operation run gradle task define above there "jacocoTestReport".
ok. all done. open the index.html in "yourapp/build/reports/jacoco/jacocoTestReport/html/".