5

I have a code and I want to put method invocation value in the name of the method.

        @Unroll
        def 'check #file other text'() {
        setup:
        def file = allProperties.keySet().getAt(0)
        ...
        where:
        ...

Now I create special variable which purpose is only for naming the method. Can I do something like:

        static def allProperties
        def setupSpec(){
           allProperties== [1: 'asd', 2: 'ddd']
        }
        @Unroll
        def 'check #allProperties.keySet().getAt(0) other text'() {
        ....
        where:
        ...

Edited: Add setupSpec()

Xelian
  • 16,680
  • 25
  • 99
  • 152
  • what good is naming your test there with something the actual test does not influence? if this test later throws on me "check lerl other text", i would expect, that lerl somehow influenced the test and that it was part of the `where` thus the reason for the unrolling. – cfrick May 18 '15 at 15:32
  • If I put it in Unroll the value of the file has to be the same for all places in the data table. – Xelian May 18 '15 at 15:35

1 Answers1

9

Unroll supports property access or zero-arg methods. So you can have:

@Unroll
def "check #allProperties.keySet().first() other text"() { .. }

provided allProperties is a class level variable or @Shared variable or mentioned under where: block.

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • Yes it is static, I tried what you say but the result is: "...Error:#allProperties.keySet().first.." but I initialize it in setupSpec() can that be the problem? – Xelian May 18 '15 at 18:24
  • 1
    can you create a simple spec script replicating the problem? I can look into it. – dmahapatro May 18 '15 at 19:31
  • You have to use `@Shared allProperties` instead. – dmahapatro May 19 '15 at 15:08
  • With `@Shared` works, but what is the difference between Shared and static? – Xelian May 19 '15 at 18:59
  • @Shared variables can be used in `where:` similar to static. But again I saw right now you are using assertion in `setupSpec` instead of instantiation. `allProperties == [1: 'asd', 2: 'ddd']`. Can you change it to `allProperties = [1: 'asd', 2: 'ddd']` and use static instead of `@Shared`. – dmahapatro May 19 '15 at 19:04
  • Refer docs for details. http://spockframework.github.io/spock/docs/1.0/index.html – dmahapatro May 19 '15 at 19:05
  • and here is a doc section, with zero-arg specifics: https://spockframework.org/spock/docs/2.0/all_in_one.html#_unrolled_iteration_names – cellepo Apr 27 '22 at 00:09