Spock框架中如何在功能方法名称中添加数字

问题描述 投票:0回答:3

我想在Spock框架的功能方法名称中添加序列号。我该怎么做?

给出代码片段以供澄清:

class Test extends Specification {

    static def count = 0;

    def setup() {
        count++
    }

    def '${count} This is first scenario'() {
        ...
    }

    def '${count} This is second scenario'() {
        ...
    }

}

运行此代码后,报告中显示:

${count} 这是第一个场景

${count} 这是第二种情况

但我期待:

1 这是第一个场景

2 这是第二种情况

string spock
3个回答
3
投票

这应该有效:

@Unroll
def "(#iterationIndex) test name" { ... }

UPD:以前称为

#iterationCount
,但现在是
#iterationIndex
,请参阅https://spockframework.org/spock/docs/2.0-M5/migration_guide.html


0
投票

这是不可能的。

def '${count} This is first scenario'()
中,您正在声明一个方法。方法名称无法动态生成。

您可以将测试的参数注入到方法名称中的模板中,使用数据驱动的测试和

@Unroll
,但我认为这不是您所需要的。

来自文档的示例:

@Unroll
def "maximum of #a and #b is #c"() { 
 expect:
    Math.max(a, b) == c

    where:
    a | b || c
    3 | 5 || 5
    7 | 0 || 7
    0 | 0 || 0
}

0
投票

使用带有 count 和 delete static 修饰符的 @Share 注释。

您可以使用 @Shared 注释向 Spock 指示您希望在所有测试方法中保留哪些对象。

© www.soinside.com 2019 - 2024. All rights reserved.