1
votes

I am writing a set of groovy scripts to be used as part of a Jenkins Pipeline Library. Currently I am using plain old JUnit to test them but would like to switch to Spock. I simply run the tests from the command line by invoking the following groovy script.

import groovy.util.AllTestSuite
import junit.textui.TestRunner

System.setProperty(AllTestSuite.SYSPROP_TEST_DIR, "./tests")
System.setProperty(AllTestSuite.SYSPROP_TEST_PATTERN, "**/*Test.groovy")

TestRunner.run(AllTestSuite.suite())

I am trying to figure what the equivalent script would be to run Spock specifications. My first attempt was to switch the SYSPROP_TEST_PATTERN to "**/*Spec.groovy. I have one ...Spec.groovy file written and sitting under ./tests that looks like this:

@Grab(group='org.spockframework', module='spock-core', version='1.0-groovy-2.3')
import spock.lang.*

class UtilsSpec extends Specification {

  def "Just testing"() {

    expect:
      1 + 1 == 2

  }
}

When I invoke my groovy script though I get:

java.lang.RuntimeException: Don't know how to treat /SourcCode/jenkins/pipeline-utils/tests/JustTestingSpec.groovy as a JUnit test

That makes sense. I need to be using Sputnik but I've looked at the Spock and Sputnik source, and the Spock example project but these all assume you are using maven or gradle. I can't figured out the right way to invoke Sputnik directly. Any ideas?

1
See this example on GitHub: gist.github.com/ysb33r/5825457 - BalRog
@BalRog, I looked at your example and its is a start but what it is missing from the above is the ability to automatically find and execute all the specs. - Kenneth Baltrinic

1 Answers

0
votes

Even though what you ask is possible and BalRog has already suggested a solution, in the long run it is better if you just use Gradle or Maven to run your tests from command line.

All Jenkins tutorials you will encounter will talk about Maven and/or Gradle and thus it would make much more sense to use a build system than custom scripts.