Skip to content

Instantly share code, notes, and snippets.

@skfarhat
Created June 26, 2018 19:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skfarhat/ce6d2a47336b1e51d23ae84dae802959 to your computer and use it in GitHub Desktop.
Save skfarhat/ce6d2a47336b1e51d23ae84dae802959 to your computer and use it in GitHub Desktop.
Sample build.gradle used in a Cucumber based test project
/*
* This file was generated by the Gradle 'init' task.
*
* This is a general purpose Gradle build.
* Learn how to create Gradle builds at https://guides.gradle.org/creating-new-gradle-builds/
*/
plugins {
id "com.github.samueltbrown.cucumber" version "0.9"
}
apply plugin: 'java'
apply plugin: 'application'
apply plugin: "jacoco"
// static analysers
apply plugin: 'checkstyle'
apply plugin: 'findbugs'
apply plugin: 'pmd'
apply plugin: 'jdepend'
// ---------------------------------------------------------------------------------------------------
// GLOBALS
// ---------------------------------------------------------------------------------------------------
version = '1.0'
mainClassName = "tax.Main"
sourceCompatibility = 1.8
targetCompatibility = 1.8
// flag
String PROPERTY_USE_TEST_IMPL = "useTestImpl"
testResultsDirName = "results/test"
//setTestImplProperty()
// The run task added by the application plugin
// is also of type JavaExec.
tasks.withType(Test) {
if (project.hasProperty(PROPERTY_USE_TEST_IMPL))
systemProperty(PROPERTY_USE_TEST_IMPL, project.getProperty(PROPERTY_USE_TEST_IMPL))
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2.2'
// junit
testCompile('org.junit.jupiter:junit-jupiter-api:5.1.0')
testCompile('org.junit.jupiter:junit-jupiter-params:5.1.0')
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.1.0')
// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.1.0'
// cucumber
testCompile group: 'junit', name: 'junit', version: '4.+'
testCompile group: 'info.cukes', name: 'cucumber-java', version: '1.2.5'
testCompile group: 'info.cukes', name: 'cucumber-core', version: '1.2.5'
testCompile group: 'info.cukes', name: 'cucumber-junit', version: '1.2.5'
testCompile group: 'info.cukes', name: 'cucumber-picocontainer', version: '1.2.5'
cucumberRuntime group: 'junit', name: 'junit', version: '4.+'
cucumberRuntime group: 'info.cukes', name: 'cucumber-java', version: '1.2.5'
cucumberRuntime group: 'info.cukes', name: 'cucumber-core', version: '1.2.5'
cucumberRuntime group: 'info.cukes', name: 'cucumber-junit', version: '1.2.5'
cucumberRuntime group: 'info.cukes', name: 'cucumber-picocontainer', version: '1.2.5'
// used for ExamplesTableGenerator
testCompile group: 'com.beust', name: 'jcommander', version: '1.72'
}
configurations {
cucumberRuntime {
extendsFrom testRuntime
}
}
jacoco {
toolVersion = "0.8.1"
reportsDir = file("$buildDir/reports/jacoco/")
}
//jacocoTestReport {
// reports {
// xml.enabled false
// csv.enabled false
// html.destination file("${buildDir}/reports/unit-coverage/")
// }
//}
// ---------------------------------------------------------------------------------------------------
// TESTING
// ---------------------------------------------------------------------------------------------------
/**
* runs unit tests
*/
test {
// Enable JUnit 5 (Gradle 4.6+).
test.useJUnitPlatform()
// Always run tests, even when nothing changed.
dependsOn 'cleanTest'
// Fail the 'test' task on the first test failure
failFast = false
ignoreFailures = true
// Show test results.
testLogging {
events "passed", "skipped", "failed"
}
jacoco {
append = false
destinationFile = file("$buildDir/results/jacoco/test.exec")
}
reports {
html.enabled = true
}
}
def jacocoAgent = zipTree(configurations.jacocoAgent.singleFile).filter { it.name == "jacocoagent.jar" }.singleFile
cucumber {
formats = ['pretty','json:build/cucumber.json', 'junit:build/cucumber.xml']
glueDirs = ['src/cucumber/java/acceptance',]
featureDirs = ['src/cucumber/resources/features']
def mystr = ""
if (project.hasProperty(PROPERTY_USE_TEST_IMPL)) {
println("in hereee")
setProperty(PROPERTY_USE_TEST_IMPL, "yes")
mystr = "-D" + PROPERTY_USE_TEST_IMPL
}
monochrome = false
strict = false
dryRun = false
ignoreFailures = true
jvmOptions {
jvmArgs "-javaagent:$jacocoAgent=destfile=$buildDir/results/jacoco/cucumber.exec,append=false"
}
}
jacocoTestReport {
executionData = files("$buildDir/results/jacoco/test.exec", "$buildDir/results/jacoco/cucumber.exec")
// executionData = files("$buildDir/results/jacoco/cucumber.exec")
reports {
xml.enabled false
csv.enabled false
html.setDestination(new File("$buildDir/reports/jacoco"))
}
// dependsOn test
// dependsOn "cucumber"
}
// ---------------------------------------------------------------------------------------------------
// CHECKS - STATIC ANALYSIS
// ---------------------------------------------------------------------------------------------------
checkstyle {
toolVersion ="8.10"
configFile = file("${rootDir}/config/checkstyle/google-checkstyle.xml")
}
findbugs() {
/* FindBugs doesn't work with JDK 1.9 */
sourceSets = [sourceSets.main]
reportsDir = file("${buildDir}/reports/findbugs")
effort = "max"
ignoreFailures = true
showProgress = true
reportLevel = "high"
visitors = ["FindSqlInjection", "SwitchFallthrough"]
omitVisitors = ["FindNonShortCircuit"]
// includeFilter = file("$rootProject.projectDir/config/findbugs/includeFilter.xml")
// excludeFilter = file("$rootProject.projectDir/config/findbugs/excludeFilter.xml")
// excludeBugsFilter = file("$rootProject.projectDir/config/findbugs/excludeBugsFilter.xml")
}
//
tasks.withType(FindBugs) {
reports {
xml.enabled false
html.enabled true
}
}
// ---------------------------------------------------------------------------------------------------
// CUSTOM TASKS
// ---------------------------------------------------------------------------------------------------
/**
* @return the String output from running 'git branch'
*/
def getGitBranch() {
return ['git', 'branch'].execute().text
}
/**
* can be called from the command line with
*
* where the value of arguments is the command line options to be passed to ExamplesTableGenerator
* ./gradlew generate_table -Parguments="-m a -o /tmp/example3.txt"
*/
task generate_table(dependsOn: 'compileTestJava') << {
javaexec {
standardOutput = System.out
classpath = sourceSets.test.runtimeClasspath
main = 'testing.acceptance.ExamplesTableGenerator'
args "$arguments".split(" ")
}
}
/**
* DEPRECATED:
*
* Compares the output of the application with the 'original' output and ensures they are the same
* This task will fail if the outputs do not match.
* The task uses MD5 hashes of several outputs and compares them with what we expect.
*
* The expectation MD5 hash has been hardcoded. It over-complicates matters to generated through code every time;
* since we know it is fixed, we just hardcode it.
*
*/
task md5_refactor_check(dependsOn: 'build') << {
// if not on the 'refactor' branch
if (getGitBranch().find("refactor") != null) {
def EXPECTED_MD5= "d04975fb875f4ddfca8162908bad584e\n" +
"3a3dc66ce5c392927df9bebc67a08076\n" +
"b242d970710df9d672105786f4c74518\n" +
"5bcacd69437f9c4afe4427ad781c92f3\n" +
"d04975fb875f4ddfca8162908bad584e\n" +
"b636448df3a1590b8b234edc5c4513e3\n" +
"806e6f8d037b30ae7b31fc967b0706cb\n" +
"193edf1fd6952dc328cdfd8797646840\n"
def aggregMD5 = ""
FileTree tree = fileTree("src/test/resources/refactor_md5")
tree.sort().each { File inFile ->
File outfile = new File("/tmp/" + inFile.toPath().getFileName())
javaexec {
standardInput = new FileInputStream(inFile)
standardOutput = new FileOutputStream(outfile)
classpath = sourceSets.main.runtimeClasspath
main = 'tax.Main'
}
def stdout = new ByteArrayOutputStream()
exec {
standardOutput = stdout
commandLine 'md5', '-q', outfile.toString()
}
aggregMD5 += stdout.toString()
}
// println(EXPECTED_MD5)
// println()
// println(aggregMD5)
// fail the build if the MD5 do not match
if (! EXPECTED_MD5.equals(aggregMD5)) {
throw new GradleException("MD5 on refactor branch does not match with expected MD5 from original")
}
}
else {
print("Not doing md5_refactor_check. We are on branch '" + getGitBranch() + "'")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment