Hello team, I have this Jenkins pipeline project ...
# community-support
a
Hello team, I have this Jenkins pipeline project and I am trying to write unit tests for some groovy code. Problem is I am not able to run a single test with
./gradlew test
Here is the high level project tree-
Copy code
.
├── build.gradle
├── gradle
│   ├── libs.versions.toml
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
├── src
│   └── com
│       └── org
│           └── SayHello.groovy
└── test
    └── com
        └── org
            └── SayHelloTest.groovy
... more in thread 🧵
my build.gradle looks like-
Copy code
plugins {
    id 'groovy'
    id 'java'
}

repositories {
    mavenCentral()
    maven { url '<https://repo.jenkins-ci.org/releases/>' }
}

sourceCompatibility = 17
targetCompatibility = 17

dependencies {
    // Use the latest Groovy version for building this library
    implementation 'org.apache.groovy:groovy-all:4.0.15'

    // Use the Jenkins Pipeline Unit for testing Jenkins pipelines
    testImplementation 'com.lesfurets:jenkins-pipeline-unit:1.20'

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.11.1'

    // Use the awesome Spock testing and specification framework
    testImplementation 'org.spockframework:spock-core:2.4-M4-groovy-4.0'
    
    // JUnit Platform launcher for Spock
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.10.0'

    // This dependency is used by the application.
    implementation 'com.google.guava:guava:32.1.2-jre'
    implementation 'org.yaml:snakeyaml:2.0'
}


// Apply a specific Java toolchain to ease working on different environments.
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

sourceSets {
    main {
        groovy {
            srcDirs = ['src/']
        }
    }
    test {
        groovy {
            srcDirs = ['test/']
        }
    }
}


test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed", "standardOut", "standardError"
        showStandardStreams = true
        exceptionFormat "full"
        afterSuite { desc, result ->
            if (!desc.parent) {
                println "\nTest result: ${result.resultType}"
                println "Test summary: ${result.testCount} tests, " +
                        "${result.successfulTestCount} succeeded, " +
                        "${result.failedTestCount} failed, " +
                        "${result.skippedTestCount} skipped"
            }
        }
    }
}
Output of test task-
Copy code
Task :test
Caching disabled for task ':test' because:
  Build cache is disabled
Task ':test' is not up-to-date because:
  Task has failed previously.
file or directory '/Users/abhishek.ratnawat/Desktop/abhishr_acquia/devops-repo/unit-testing-groovy/build/classes/java/test', not found
Starting process 'Gradle Test Executor 57'. Working directory: /Users/abhishek.ratnawat/Desktop/abhishr_acquia/devops-repo/unit-testing-groovy Command: /Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home/bin/java -Dorg.gradle.internal.worker.tmpdir=/Users/abhishek.ratnawat/Desktop/abhishr_acquia/devops-repo/unit-testing-groovy/build/tmp/test/work @/Users/abhishek.ratnawat/.gradle/.tmp/gradle-worker-classpath9043141152934101892txt -Xmx512m -Dfile.encoding=UTF-8 -Duser.country=IN -Duser.language=en -Duser.variant -ea worker.org.gradle.process.internal.worker.GradleWorkerMain 'Gradle Test Executor 57'
Successfully started process 'Gradle Test Executor 57'

Test result: SUCCESS
Test summary: 0 tests, 0 succeeded, 0 failed, 0 skipped
Finished generating test XML results (0.0 secs) into: /Users/abhishek.ratnawat/Desktop/abhishr/devops-repo/unit-testing-groovy/build/test-results/test
Generating HTML test report...
Finished generating test html results (0.014 secs) into: /Users/abhishek.ratnawat/Desktop/abhishr/devops-repo/unit-testing-groovy/build/reports/tests/test
Can someone please help me, what is wrong here?
./gradlew clean test --info
doesn't fails but 0 tests executed
v
What's the content of
SayHelloTest.groovy
?
a
Content of SayHello.groovy
Content of SayHelloTest.groovy
v
You code a Jupiter test, but don't have Jupiter engine available. You only have Jupiter API available, and Spock engine. Either code a Spock test (my recommendation) or add the Jupiter engine as test runtime dependency.
a
I prefer using JenkinsPipelineUnit framework so I created the tests accordingly. I added these dependencies but still no tests are running-
Copy code
dependencies {
    implementation 'org.apache.groovy:groovy-all:4.0.15'
    testImplementation 'com.lesfurets:jenkins-pipeline-unit:1.20'
    implementation 'org.yaml:snakeyaml:2.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.10.5'
}
Btw, which spock you are referring? Till now I was using jenkins-spock: https://github.com/ExpediaGroup/jenkins-spock but now it's dead project and we wanted to look into some other easy to implement framework.
Is spockframework https://github.com/spockframework/spock a good framework for writing Jenkins pipeline unit tests??
Or any other recommended framework for this purpose?
v
I have no idea about Jenkins in specific. But yes, that is the Spock I mentioned and that you also included with
Copy code
// Use the awesome Spock testing and specification framework
testImplementation 'org.spockframework:spock-core:2.4-M4-groovy-4.0'
Generally it is an awesome Test framework with which testing became fun again for me.