-
Notifications
You must be signed in to change notification settings - Fork 18
Available Test Types
Uses Project Builder to create a in-memory expression of a Gradle build (project variable), specifically in a 'projectDir'. A sanitized project name will be stored in canonicalName. Caveat, this is ONLY setting up the Project data structure, and not running through the completely lifecycle, Like to see http://issues.gradle.org/browse/GRADLE-1619. Its value lays in being able to execute method with a proper Project object, which can flush out most groovy functions, finding basic compiler like issues. The private method evaluate() can be called on the project variable for force evaluation of afterEvaluate blocks, keeping in mind that still won't generate a task gradle or run the tasks.
Example:
package nebula.example
import java.util.concurrent.atomic.AtomicBoolean
class ConcreteProjectSpec extends ProjectSpec {
def 'has Project'() {
expect:
project != null
}
def 'can evaluate'() {
setup:
def signal = new AtomicBoolean(false)
project.afterEvaluate {
signal.getAndSet(true)
}
when:
project.evaluate()
then:
noExceptionThrown()
signal.get() == true
}
}
Small abstraction over ProjectSpec for plugins, adds three tests that ensure the plugin can be applied properly (idempotently and in a multi-project).
Example:
package nebula.example
class PluginProjectExampleSpec extends PluginProjectSpec {
@Override
String getPluginName() { return 'plugin-example' }
}
Orchestrate a Gradle build via GradleLauncher, which is deprecated, or the Tooling API to perform a high-level integration test of a project. Each test gets it's own test directory, called projectDir. It is up to the implementer to add contents to the buildFile and the settingsFile. The project's name is available as moduleName, which is a sanitized version of the test's name.
The spec will assume the Tooling API, but this can be changed by setting useToolingApi to false in which case the GradleLauncher will be used. It's risky to use since it's not support, but it provide more details from the resulting build. Though if you're checking the contents of the projectDir after build, it shouldn't matter. The GradleLauncher is required to debug the running of your build, you can't set break points in the build.gradle file, but you can set them in the plugins being called.
It's up to your test to call the runTask methods. There are a few utility methods to help assemble a project.
logLevel |
Adjust log level being used |
fork |
By default tests are executed in the same JVM as Gradle which is helpful for debugging your code. If you want classloader isolation, you might want to rather go with a forked JVM for executing your tests. Note: This flag is based on an property from Gradle’s non-public API. |
File directory(String path) |
Create a directory, with a mkdirs. |
File createFile(String path) |
Create a file, relative from the project, with parent directories being created. |
def writeHelloWorld(String packageDotted) |
Write out a simple java HelloWorld in the package provided |
String copyResources(String srcDir, String destination) |
Copy a resource from the classpath to the project’s directory |
String applyPlugin(Class pluginClass) |
Returns the appropriate string for applying a plugin, using a loadClass call. Would need to be added to the buildFile |
BuildResult analyze(String… tasks) |
Analysis of project with the given tasks, doesn’t actually execute tasks |
BuildResult runTasksWithSuccessfully(String… tasks) |
Run, and assume that the build will succeed. |
BuildResult runTasksWithFailure(String… tasks) |
Run, and assume that the build will fail. |
boolean fileExists(String path) |
Says if a file was created in the project dir |
File addSubproject(String subprojectName) |
Create a subproject, return back the new directory |
File addSubproject(String subprojectName, String subBuildGradleText) |
Create a subproject setting its build.gradle to the given String |
boolean wasExecuted(String taskPath) |
Says if a task was executed. |
boolean wasUpToDate(String taskPath) |
Says if a task was recorded as UP-TO-DATE. |
String getStandardError() |
Returns System.err which can be inspected |
String getStandardOutput() |
Returns System.out which can be inspected |
Throwable getFailure() |
Returns the Throwable available to failed builds. |
Example:
package nebula.example
import nebula.test.functional.ExecutionResult
class ConcreteIntegrationSpec extends IntegrationSpec {
def 'runs build'() {
when:
ExecutionResult result = runTasks('dependencies')
then:
result.failure == null
}
def 'setup and run build'() {
writeHelloWorld('nebula.hello')
buildFile << '''
apply plugin: 'java'
'''.stripIndent()
when:
ExecutionResult result = runTasksSuccessfully('build')
then:
fileExists('build/classes/main/nebula/hello/HelloWorld.class')
result.standardOutput.contains(':compileTestJava')
}
}