Add method call in test name with Unroll using Spock
Asked Answered
U

1

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()

Useful answered 18/5, 2015 at 15:18 Comment(2)
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.Subtrahend
If I put it in Unroll the value of the file has to be the same for all places in the data table.Useful
A
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.

Allare answered 18/5, 2015 at 16:29 Comment(7)
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?Useful
can you create a simple spec script replicating the problem? I can look into it.Allare
You have to use @Shared allProperties instead.Allare
With @Shared works, but what is the difference between Shared and static?Useful
@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.Allare
Refer docs for details. spockframework.github.io/spock/docs/1.0/index.htmlAllare
and here is a doc section, with zero-arg specifics: spockframework.org/spock/docs/2.0/…Andeee

© 2022 - 2024 — McMap. All rights reserved.