Skip to content

Commit 03efcb0

Browse files
committedFeb 10, 2015
Tests for tests in seperate project
Currently report is generated, but coverage is wrong, so tests fail.
1 parent 10a38dd commit 03efcb0

File tree

6 files changed

+138
-0
lines changed

6 files changed

+138
-0
lines changed
 

Diff for: ‎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,41 @@
1+
package org.scoverage
2+
3+
import org.hamcrest.core.Is
4+
import org.junit.Assert
5+
import org.junit.Test
6+
7+
class SeparateTestsAcceptanceTest extends AcceptanceTestUtils {
8+
9+
private void testSeparate(boolean useAnt) throws Exception {
10+
File projectDir = new File('src/test/separate-tests')
11+
File subprojectDir = new File(projectDir, 'a')
12+
File testsSubprojectDir = new File(projectDir, 'a-tests')
13+
14+
def build = setupBuild(projectDir, useAnt)
15+
build.forTasks('clean', 'reportScoverage').run()
16+
17+
// ensure report is generated in base project ...
18+
def indexHtml = new File(reportDir(subprojectDir), 'index.html')
19+
checkFile('an index HTML file', indexHtml, true)
20+
21+
// ... but not in test project ...
22+
def testsIndexHtml = new File(reportDir(testsSubprojectDir), 'index.html')
23+
checkFile('an index HTML file', testsIndexHtml, false)
24+
25+
// ... and both statement and branch coverage is 100%
26+
def branchCoverage = coverage(reportDir(subprojectDir), CoverageType.Branch)
27+
def statementCoverage = coverage(reportDir(subprojectDir), CoverageType.Statement)
28+
Assert.assertThat('Branch coverage should be 100%, was ' + branchCoverage, branchCoverage, Is.is(100.0))
29+
Assert.assertThat('Statement coverage should be 100%, was ' + statementCoverage, statementCoverage, Is.is(100.0))
30+
}
31+
32+
@Test
33+
public void testSeparateTestsWithAnt() throws Exception {
34+
testSeparate(true)
35+
}
36+
37+
@Test
38+
public void testSeparateTestsWithZinc() throws Exception {
39+
testSeparate(false)
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package hello
2+
3+
import org.junit.Test
4+
import org.junit.Assert.assertEquals
5+
6+
class HelloTest {
7+
8+
@Test def testText() {
9+
assertEquals("Hello World", new Hello().text)
10+
}
11+
12+
}
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+
}

Diff for: ‎src/test/separate-tests/build.gradle

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}
39+
40+
configure(subprojects.findAll { it.name.endsWith('-tests') }) {
41+
def mainProject = project(":${project.name.minus('-tests')}")
42+
dependencies {
43+
testCompile mainProject
44+
}
45+
scoverage {
46+
sources = mainProject.extensions.scoverage.sources
47+
dataDir = mainProject.extensions.scoverage.dataDir
48+
reportDir = mainProject.extensions.scoverage.reportDir
49+
}
50+
testScoverage {
51+
classpath = mainProject.sourceSets.scoverage.runtimeClasspath + sourceSets.test.runtimeClasspath
52+
}
53+
}

Diff for: ‎src/test/separate-tests/settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include ':a', ':a-tests'

0 commit comments

Comments
 (0)
Please sign in to comment.