Skip to content

Commit 1f23733

Browse files
committed
Merge pull request #43 from Kwestor/tests/separate-tests
Support for tests in separate project
2 parents 7212400 + 7268df0 commit 1f23733

File tree

9 files changed

+173
-0
lines changed

9 files changed

+173
-0
lines changed

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ dependencies {
4343
compile gradleApi()
4444
compile localGroovy()
4545
scoverage 'org.scoverage:scalac-scoverage-plugin_2.11:1.0.2'
46+
testCompile 'org.hamcrest:hamcrest-library:1.3'
4647
}
4748

4849
task groovydocJar(type: Jar, dependsOn: groovydoc) {

src/main/groovy/org/scoverage/ScoverageExtension.groovy

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import org.gradle.api.plugins.JavaPlugin
99
import org.gradle.api.plugins.scala.ScalaPlugin
1010
import org.gradle.api.tasks.JavaExec
1111
import org.gradle.api.tasks.SourceSet
12+
import org.gradle.api.tasks.bundling.Jar
1213
import org.gradle.api.tasks.testing.Test
1314
import org.gradle.util.GFileUtils
1415

@@ -88,6 +89,15 @@ class ScoverageExtension {
8889
runtimeClasspath = it.output + mainSourceSet.output + project.configurations.scoverage + project.configurations.testRuntime
8990
}
9091

92+
def scoverageJar = project.tasks.create('jarScoverage', Jar.class) {
93+
dependsOn('scoverageClasses')
94+
classifier = ScoveragePlugin.CONFIGURATION_NAME
95+
from mainSourceSet.output
96+
}
97+
project.artifacts {
98+
scoverage project.tasks.jarScoverage
99+
}
100+
91101
project.tasks.create(ScoveragePlugin.TEST_NAME, Test.class) {
92102
conventionMapping.map("testClassesDir", new Callable<Object>() {
93103
public Object call() throws Exception {

src/test/groovy/org/scoverage/AcceptanceTestUtils.groovy

+26
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,32 @@ import org.gradle.tooling.GradleConnector
55
import org.hamcrest.core.Is
66
import org.junit.Assert
77

8+
enum CoverageType {
9+
Line('cobertura.xml', 'line-rate'),
10+
Statement('scoverage.xml', 'statement-rate'),
11+
Branch('scoverage.xml', 'branch-rate')
12+
13+
String fileName
14+
String paramName
15+
16+
CoverageType(String fileName, String paramName) {
17+
this.fileName = fileName
18+
this.paramName = paramName
19+
}
20+
}
21+
822
/**
923
* Some utils for easy acceptance testing.
1024
*/
1125
class AcceptanceTestUtils {
1226

27+
XmlParser parser
28+
29+
AcceptanceTestUtils() {
30+
parser = new XmlParser()
31+
parser.setFeature('http://apache.org/xml/features/disallow-doctype-decl', false)
32+
parser.setFeature('http://apache.org/xml/features/nonvalidating/load-external-dtd', false)
33+
}
1334

1435
protected BuildLauncher setupBuild(File projectRoot, boolean useAnt) {
1536
return GradleConnector.
@@ -28,4 +49,9 @@ class AcceptanceTestUtils {
2849
return new File(baseDir, 'build/reports/scoverage')
2950
}
3051

52+
protected Double coverage(File reportDir, CoverageType coverageType) {
53+
File reportFile = new File(reportDir, coverageType.fileName)
54+
def xml = parser.parse(reportFile)
55+
xml.attribute(coverageType.paramName).toDouble()
56+
}
3157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.scoverage
2+
3+
import static org.hamcrest.number.IsCloseTo.closeTo
4+
import org.junit.Test
5+
6+
import static org.junit.Assert.assertThat
7+
8+
class SeparateTestsAcceptanceTest extends AcceptanceTestUtils {
9+
10+
private void testSeparate(boolean useAnt) throws Exception {
11+
File projectDir = new File('src/test/separate-tests')
12+
File subprojectDir = new File(projectDir, 'a')
13+
File testsSubprojectDir = new File(projectDir, 'a-tests')
14+
15+
def build = setupBuild(projectDir, useAnt)
16+
build.forTasks('clean', 'reportScoverage').run()
17+
18+
// ensure report is generated in base project ...
19+
def indexHtml = new File(reportDir(subprojectDir), 'index.html')
20+
checkFile('an index HTML file', indexHtml, true)
21+
22+
// ... but not in test project ...
23+
def testsIndexHtml = new File(reportDir(testsSubprojectDir), 'index.html')
24+
checkFile('an index HTML file', testsIndexHtml, false)
25+
26+
// ... and 'Hello.scala' is present there ...
27+
def helloHtml = new File(reportDir(subprojectDir), 'src/main/scala/hello/Hello.scala.html')
28+
checkFile('Hello.scala html file', helloHtml, true)
29+
30+
// ... and both statement and branch coverage is 100%
31+
def branchCoverage = coverage(reportDir(subprojectDir), CoverageType.Branch)
32+
def statementCoverage = coverage(reportDir(subprojectDir), CoverageType.Statement)
33+
assertThat('Branch coverage should be 100%, was ' + branchCoverage, branchCoverage, closeTo(100.0, 1.0))
34+
assertThat('Statement coverage should be 100%, was ' + statementCoverage, statementCoverage, closeTo(100.0, 1.0))
35+
}
36+
37+
@Test
38+
public void testSeparateTestsWithAnt() throws Exception {
39+
testSeparate(true)
40+
}
41+
42+
@Test
43+
public void testSeparateTestsWithZinc() throws Exception {
44+
testSeparate(false)
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package hello
2+
3+
class BaseTest {
4+
def beforeTest() = {
5+
println("Running test!")
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package hello
2+
3+
import org.junit.Test
4+
import org.junit.Assert.assertEquals
5+
6+
class HelloTest extends BaseTest {
7+
8+
@Test def testText() {
9+
beforeTest()
10+
assertEquals("Hello World", new Hello().text)
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package hello
2+
3+
class Hello {
4+
def text = "Hello World"
5+
}

src/test/separate-tests/build.gradle

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
description = 'a multi-project with separate tests setup for gradle-scoverage'
2+
3+
buildscript {
4+
repositories {
5+
// need to get up to the working directory of gradle-plugins build
6+
flatDir dir: "${project.projectDir}/../../../build/libs"
7+
}
8+
dependencies {
9+
classpath name: 'gradle-scoverage', version: '+'
10+
}
11+
}
12+
13+
subprojects {
14+
15+
repositories {
16+
mavenCentral()
17+
}
18+
19+
apply plugin: 'scala'
20+
apply plugin: 'scoverage'
21+
22+
dependencies {
23+
compile 'org.scala-lang:scala-library:2.11.4'
24+
scoverage 'org.scoverage:scalac-scoverage-plugin_2.11:1.0.4',
25+
'org.scoverage:scalac-scoverage-runtime_2.11:1.0.4'
26+
27+
testCompile 'junit:junit:4.11'
28+
}
29+
30+
testScoverage {
31+
onlyIf { project.name.endsWith('-tests') }
32+
}
33+
34+
reportScoverage {
35+
onlyIf { project.name.endsWith('-tests') }
36+
}
37+
38+
checkScoverage {
39+
onlyIf { project.name.endsWith('-tests') }
40+
}
41+
42+
}
43+
44+
configure(subprojects.findAll { it.name.endsWith('-tests') }) {
45+
def mainProject = project(":${project.name.minus('-tests')}")
46+
dependencies {
47+
testCompile mainProject
48+
scoverage mainProject.configurations.scoverage.artifacts.files
49+
}
50+
scoverage {
51+
sources = mainProject.extensions.scoverage.sources
52+
dataDir = mainProject.extensions.scoverage.dataDir
53+
reportDir = mainProject.extensions.scoverage.reportDir
54+
}
55+
sourceSets {
56+
testScoverage {
57+
compileClasspath += sourceSets.main.output
58+
runtimeClasspath += sourceSets.main.output
59+
}
60+
}
61+
compileScoverageScala {
62+
onlyIf { false }
63+
}
64+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include ':a', ':a-tests'

0 commit comments

Comments
 (0)