Create a helper method in the spock test that will not run as a test

I am a java developer changing the groovy test, so I apologize if I am not quite aware of the basic concepts and terminology of groovy (I think I should say that instead of the method I need to close here)?

In any case, my desire is simple, I want to create some helper methods that can be reused in an existing groovy test. Things like starting / stopping resources that are needed only for certain tests (and will be broken down into others), or faster to complete the general testing phase.

However, it seems that at any moment when I try to create a method (closing?) Like this, it starts spock as a test that breaks other tests in funny ways. Is there an easy way to add a method to my test tests without running it as a test?

It seems like a static action might work, but there are methods that I want to touch the variables of the @shared class, so I'm not sure if the static option is the best option.

+4
source share
1 answer

I heavily used traits to write unit tests.

Each trait introduces test functions into the specification.

trait Helper {
    def helpTests() {
        println "Starting Test"
        return 2
    }
}

class MySpec extends Specification
        implements Helper {
    def "my spec"() {
        when:
        def n = helpTests()

        then:
        n == 2
    }
}

Attribute methods are not implemented, obviously.

+7
source

All Articles