saladinkzn / gretty

Advanced gradle plugin for running web-apps on jetty and tomcat.
MIT License
2 stars 2 forks source link

using integration tests with gretty in combination with cobertura #6

Closed x12a1f closed 8 years ago

x12a1f commented 8 years ago

Hi,

Now that I've got gretty working with jacoco I'm kind of disappointed with jacoco and I want to give cobertura a try.

I believe this should do the trick:

afterEvaluate {
    tasks.appBeforeIntegrationTest {
        gretty { 
              classPath "${project.buildDir}/instrumented_classes"
        }
    }
}

but it does not seem to work as it adds the instrumented_classes to the end of the classpath. For cobertura to work the instrumented_classes need to be at the front of the classpath (or the default build/classes/main entry removed from the classpath)

Do you have any idea or example on how to get cobertura to work with integration tests?

saladinkzn commented 8 years ago

Hello,

I'm afraid there's no way to do it right now as project.sourceSets.main.output is always added in front of classpath.

The only way I can see to achieve this is to put instrumented classes in sourceSets.main.output (build/main/classes) folder overriding default classes.

saladinkzn commented 8 years ago

Furthermore I've added support for beforeClassPath in gretty-1.2.6-SNAPSHOT:

gretty {
    beforeClassPath "${buildDir}/instrumentedClasses"
}
x12a1f commented 8 years ago

Thanks. I tried it but it does not seem to be working. When using beforeClassPath it still uses the normal, uninstrumented, code.

Currently I have it working using the following work around:

cobertura {
    // cobertura in the servlet containter started by gretty looks for the datafile in this
    // location. TODO: pass the location of this file to the servlet using system properties
    coverageOutputDatafile = file("$projectDir/cobertura.ser")
    coverageReportDatafile = file("$projectDir/cobertura.ser")
}

afterEvaluate {
    tasks.appBeforeIntegrationTest {
        gretty {
            servletContainer = 'jetty9'

            // add the instrumented classes to the class path
            classPath "${project.buildDir}/instrumented_classes"

            // add the libraries needed by cobertura to the class path
            configurations.cobertura.each {
                classPath it
            }

        }
    }
    .doFirst {
        // move the classes out of the way so gretty can only see the instrumented_classes
        file("${project.buildDir}/classes/main.nope").deleteDir()
        file("${project.buildDir}/classes/main").renameTo("${project.buildDir}/classes/main.nope")
    }
}

afterEvaluate {
    tasks.appAfterIntegrationTest.finalizedBy 'generateCoberturaReport'

    // move the classes back
    tasks.generateCoberturaReport.doFirst {
        file("${project.buildDir}/classes/main.nope").renameTo("${project.buildDir}/classes/main")
    }
}
saladinkzn commented 8 years ago

Nice to hear you found a workaround. I'll take a look again today.

saladinkzn commented 8 years ago

Well, using following configuration I was able to achive success with gretty-1.2.6-SNAPSHOT

buildscript {
  repositories {
    mavenLocal()
    jcenter()
    mavenCentral()
  }
  dependencies {
    classpath "ru.shadam.gretty:gretty:$gretty_version"
    classpath "net.saliman:gradle-cobertura-plugin:2.3.1"
  }
}

repositories {
  mavenLocal()
  jcenter()
  maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local' }
}

apply plugin: 'groovy'
apply plugin: 'org.akhikhl.gretty'
apply plugin: 'net.saliman.cobertura'

dependencies {
  compile 'org.webjars:bootstrap:3.2.0'
  compile 'org.webjars:jquery:2.1.1'
  // We use Velocity for example of template processing within the webapp.
  compile 'org.apache.velocity:velocity:1.7'
}

cobertura {
  coverageOutputDatafile = file("$projectDir/cobertura.ser")
  coverageReportDatafile = file("$projectDir/cobertura.ser")
}

afterEvaluate {
  tasks.appBeforeIntegrationTest {
    gretty {
      servletContainer = 'jetty9'

      jvmArgs = ['-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005']

      beforeClassPath "${project.buildDir}/instrumented_classes/"

      // add the libraries needed by cobertura to the class path
      configurations.cobertura.each {
        classPath it
      }
    }
  }

  tasks.appAfterIntegrationTest.finalizedBy 'coberturaReport'
}

dependencies {
  testCompile "org.codehaus.groovy:groovy-all:$groovy_version"
  testCompile "org.spockframework:spock-core:$spock_version"
  testCompile "org.gebish:geb-spock:$gebVersion"
  testCompile "org.seleniumhq.selenium:selenium-support:$seleniumVersion"
  testCompile "org.seleniumhq.selenium:selenium-firefox-driver:$seleniumVersion"
}

test {
  include '**/Test*.*'
  include '**/*Test.*'
  exclude '**/*IT.*'
}

task integrationTest(type: Test, dependsOn: 'test') {
  outputs.upToDateWhen { false }
  include '**/*IT.*'
  doFirst {
    systemProperty 'geb.build.reportsDir', reporting.file('geb')
  }
}

You should use

maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local' }

instead of mavenLocal() to reproduce my build.

x12a1f commented 8 years ago

Yes, I'm not sure what I did wrong earlier but it works. Many thanks!