Grails 4 Code Coverage

125 views
Skip to first unread message

Jessie Evangelista

unread,
Aug 30, 2019, 3:00:13 AM8/30/19
to Grails Dev Discuss
Hi all,

We are considering using grails 4.0.0 for a new product and we'd like to do it the right way from the start. 

It seems there are no plugins yet for code coverage in the grails3 list:
 
The plugins for grails 1 and 2 also dont work:
OpenClover Code Coverage for Grails
Test Code Coverage Plugins

What is the best way to implement code coverage measurement for a grails4 web application?


Jeff Scott Brown

unread,
Aug 30, 2019, 11:07:30 AM8/30/19
to Grails Dev Discuss
JaCoCo is quite popular.

https://docs.gradle.org/current/userguide/jacoco_plugin.html



JSB
--
Jeff Scott Brown
Partner and Practice Lead, Grails and Micronaut

Disruptive solutions for a connected world.™
http://objectcomputing.com

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

Jeff Scott Brown

unread,
Aug 30, 2019, 11:18:50 AM8/30/19
to Grails Dev Discuss
On 30 Aug 2019, at 10:07, Jeff Scott Brown wrote:

> On 30 Aug 2019, at 2:00, Jessie Evangelista wrote:
>
>> Hi all,
>>
>> We are considering using grails 4.0.0 for a new product and we'd like
>> to do
>> it the right way from the start.
>>
>> It seems there are no plugins yet for code coverage in the grails3
>> list:
>> http://plugins.grails.org/?query=coverage&submit=Search
>>
>> The plugins for grails 1 and 2 also dont work:
>> OpenClover Code Coverage for Grails
>> Test Code Coverage Plugins
>>
>> What is the best way to implement code coverage measurement for a
>> grails4
>> web application?
>>
>
> JaCoCo is quite popular.
>
> https://docs.gradle.org/current/userguide/jacoco_plugin.html
>
>
>

FYI… I had not used it with Grails 4 yet but just created a simple app
to see if it appears to work and it does.

I just created a new app and added the following to build.gradle:

plugins {
id 'jacoco'
}


jacocoTestReport {
executionData test, integrationTest
}


That seems to generate the expected reports.

Jessie Evangelista

unread,
Sep 2, 2019, 12:27:32 AM9/2/19
to Grails Dev Discuss
Thank you for this. here is what I did with it:
grails create-app foo-app && cd foo-app
cat >> build.gradle << 'EOF'
apply plugin:"jacoco"
jacoco {
    toolVersion = "0.8.4"
}
jacocoTestReport {
    dependsOn test
    reports {
        xml.enabled false
        csv.enabled false
        html.destination file("${buildDir}/reports/jacocoHtml")
    }
}
EOF

mkdir -p grails-app/domain/foo/app/
cat >> grails-app/domain/foo/app/Bar.groovy << EOF
package foo.app

class Bar {
    String name

    static constraints = {
    }
}
EOF
mkdir -p src/test/groovy/foo/app/
cat >> src/test/groovy/foo/app/BarSpec.groovy << EOF
package foo.app

import grails.testing.gorm.DomainUnitTest
import spock.lang.Specification

class BarSpec extends Specification implements DomainUnitTest<Bar> {

    def setup() {
    }

    def cleanup() {
    }

    void "test should have name property"() {
        Bar bar = new Bar()
        bar.name = "valid"
        expect:"name should be retained"
        "valid".equals(bar.name)
    }
}
EOF
grails gradle jacocoTestReport
firefox build/reports/jacocoHtml/index.html

Which got me the image below:

Screenshot at 2019-08-30 18-23-44.png


As can be seen, jacoco includes the injected methods in coverage computation.
Is there a quick way to exclude the generated methods?


 
Reply all
Reply to author
Forward
0 new messages