On Wed, May 11, 2011 at 9:49 PM, Adam Murdoch
<[email protected]>wrote:
>
> On 12/05/2011, at 1:51 AM, Brett Cave wrote:
>
> Would it be possible (how would I... ) use gradle to run JUnit tests? The
> primary reason for this is for pre-deploy integration tests outside of build
> environment. I have configured a project to package all the dependencies to
> run stand-alone integration testing, and would like to have consistency in
> the reporting provided by gradle in build-time unit / component testing.
>
> My current methodology for this is to compile and archive main and test
> classes and then run by calling org.junit.runner.JUnitCore. Ideally, I
> wouldn't want to have environment dependencies on gradle in environments
> other than build (i.e. a method of doing this will a fully portable test
> suite).
>
>
>
> It is, however, easy to use the Test task type without using the Java
> plugin, so you could put together a Gradle project which simply defines a
> test task. Or you could just add another Test instance to an existing
> project.
>
Thanks, I took this suggestion, as it seemed the best way to go about it.
Gradle runs great without needing any setup for repo's required without the
extra plugins added, its a straightforward build script. However, my test
execution fails, it seems to not find the test cases from the suite)
build.gradle (that is distributed with the IT package, which is different to
the the project's build.gradle):
/* start build.gradle */
// contains all dependencies.
libDir="libs"
testJar="my-project-1.0.0-SNAPSHOT-integration-test.jar"
mainJar="my-project-1.0.0-SNAPSHOT.jar"
tmpDir="tmp"
task integrationTest(type: Test, dependsOn: "setupTest") {
// test classes in tmpDir
classpath = files(mainJar, tmpDir)
new File(libDir).list().each {
classpath += files(it)
}
testClassesDir = file(tmpDir)
testResultsDir = file("${buildDir}/results")
testReportDir = file("${buildDir}/reports/tests")
include '**/IntegrationTestSuite.*'
}
// http://issues.gradle.org/browse/GRADLE-863 - extract to temp directory
task setupTest << {
println "Setting up tests"
// couldn't figure out how to unjar with gradle directly :/
ant.unzip(dest: tmpDir, src: testJar)
}
/* end build.gradle */
When I run this, I get the following error:
<failure message="java.lang.Exception: No runnable methods"
type="java.lang.Exception">java.lang.Exception: No runnable methods
at
org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:177)
at
org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:122)
IntegrationTestSuite.java (this runs fine when I run a custom
"integrationTest" task from the project's build.gradle, so the structure of
the test suite / test cases is valid. It also runs fine with JUnit directly,
see below the code snippets):
@RunWith(Suite.class)
@Suite.SuiteClasses({
MyServiceTestCase.class
})
public class DeployStageIntegrationTestSuite {
}
MyServiceTestCase.java (simplified):
public class MyTestCase {
@Test
public void serviceTest() {
// test method.
}
}
This test runs fine if I run with JUnit directly:
# $TESTCP is built up in the same way as the integrationTest classpath above
is, with the testJar on the CP and no tmpDir
java -cp "$TESTCP" org.junit.runner.JUnitCore
com.mycompany.test.integration.suite.IntegrationTestSuite
The debugging on gradle seems to have the CP set correctly (am doing this
from /tmp/it):
[DEBUG] [org.gradle.process.internal.DefaultWorkerProcessFactory] Using
application classpath [/tmp/it/my-project-1.0.0-SNAPSHOT.jar, /tmp/it/tmp,
<the rest of $TESTCP here> ]
>
> --
> Adam Murdoch
> Gradle Co-founder
> http://www.gradle.org
> VP of Engineering, Gradleware Inc. - Gradle Training, Support, Consulting
> http://www.gradleware.com
>
>
--
Brett Cave
Systems Architect
Jemstep, Inc
www.jemstep.com