Java Programming Tutorials

Java programming tutorials with many code examples!

Flexibly ignore tests in Spock

Problem:

How to flexibly ignore tests in Spock Framework? In this post we’re going to show Spock’s flexible extensions that can spare you a headache. Read on!

What!? Ignoring tests!? Yes! In real life there is a number of situations when one would like to do that:

  • It’s end of the day and the test is failing on CI server. You can ignore it for the night and fix it as the first thing on the next day.
  • A test should be run only on selected operating system. For example some tests work only on Linux, because they expect Linux specific directory to exist. On other systems the test should be ignored.
  • A test should be run only when some environment variable is present. For example it can be a variable set only on Continuous Integration server.

 

Solution:

In the following example we’re going to show how to flexibly ignore tests in Spock Framework using its built-in extensions:

  • @Ignore
    Ignores annotated test or whole specification.
  • @IgnoreIf(predicate)
    Ignores annotated test or specification, but only when given predicate is true.
  • @IgnoreRest
    Runs annotated test and ignores all other tests in the same specification.
  • @Requires(predicate)
    Runs annotated test or specification, but only when given predicate is true.

 

package com.farenda.spock

import spock.lang.Ignore
import spock.lang.IgnoreIf
import spock.lang.Requires
import spock.lang.Specification

import java.nio.file.Files
import java.nio.file.Paths

//@Ignore, @IgnoreIf, @Requires can be on whole Spec too!
class IgnoringTestsInSpockTest extends Specification {

    // The reason is optional, but a good practice:
    @Ignore('Have to verify equation with BA')
    def 'should not execute no matter what'() {
        expect:
        Math.pow(2, 2) == 5
    }

    // @IgnoreIf( true is returned )
    @IgnoreIf({ System.getProperty('os.name').contains('windows') })
    def 'should not run on Windows'() {
        expect:
        Files.exists(Paths.get('/tmp'))
    }

    // using builtin "os" - info about operating system
    // see: spock.util.environment.OperatingSystem
    @IgnoreIf({ os.linux })
    def 'should not run on Linux - shorter version'() {
        expect:
        Files.exists(Paths.get('c://windows'))
    }

    // @Requires is oposite to @IgnoreIf, so runs when condition holds
    // see: spock.util.environment.OperatingSystem
    @Requires({ os.linux })
    def 'should run only on Linux - shorter version'() {
        expect:
        Files.exists(Paths.get('/tmp'))
    }

    // using builtin "sys" - a map of system properties
    // see: https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
    @IgnoreIf({ sys['user.name'] != 'Spock' })
    def 'should not run when user is not "Spock"'() {
        expect:
        Files.exists(Paths.get('/home/Spock'))
    }

    // using builtin "env" - a map of environment variables
    @IgnoreIf({ env.HOME != '/home/Spock' })
    def 'should not run when user home dir is different'() {
        expect:
        Files.exists(Paths.get('/home/Spock'))
    }

    // using builtin "jvm" - info about JVM
    // see: spock.util.environment.Jvm
    @IgnoreIf({ !jvm.java8Compatible })
    def 'should run only when JDK is compatible with Java 8'() {
        expect:
        System.properties['java.version'] == '1.8.0_66'
    }
}

As you can see @IgnoreIf and @Requires are like Yin and Yang.

Here’s how it looks like in an IDE:

Ignore tests in Spock

Ignore tests in Spock

In the following example we’re going to show how @IgnoreRest can be used to ignore all other tests:

package com.farenda.spock

import spock.lang.IgnoreRest
import spock.lang.Specification

class IgnoringOtherTest extends Specification {

    def 'this one will not run'() {
        expect:
        Math.abs(-1) == -1
    }

    @IgnoreRest
    def 'this one will run'() {
        expect:
        Math.sin(0) == 0
    }

    def 'this one will be ignored too'() {
        expect:
        Math.pow(2, 2) == 3
    }
}

And here’s the result:

Ignore rest of tests in Spock

Ignore rest of tests in Spock

This I like. :-) Especially the ways to exclude tests depending on available operating system.

Share with the World!