Spock:如何在数据管道中输入字符串值

时间:2016-09-07 09:49:01

标签: testing groovy automation spock geb

我google了很多,但没有找到在数据管道中使用字符串值填充输入数据的单个示例。

有人能举例说明怎么做吗?

我用过:

[a,b,c] << ["aaa","bbb","ccc"] 

但是收到错误。

2 个答案:

答案 0 :(得分:2)

简单地将多个值分配给多个变量就像这样:

def (a, b, c) = ["aaa", "bbb", "ccc"]

但Spock的输入数据更像是这样:

where:
a << [3, 7, 0]
b << [5, 0, 0]
c << [5, 7, 0]

http://spockframework.org/spock/docs/1.0/data_driven_testing.html

答案 1 :(得分:1)

假设您要运行测试3次,并指定相同的变量&#34; aaa&#34;第一次运行,&#34; bbb&#34;对于第二个,&#34; ccc&#34;对于第三个,你会这样做:

where:
variableName << ["aaa","bbb","ccc"] 

以下是我的一个项目的完整示例:

class PhoneNumberConverterSpec extends Specification {

    private PhoneNumberConverter phoneNumberConverter = PhoneNumberConverter.instance

    @Unroll('convert invalid phone number #input')
    def 'attempt to convert invalid phone numbers to canonical format'() {

        expect:
        !phoneNumberConverter.canConvert(input)

        where:
        input << [null, 6, '353', '9999999']
    }
}

另一方面,如果你想只运行一次测试并将值分配给3个不同的变量那么使用数据管道真的没有意义,只需像在常规Groovy中那样声明和分配变量代码。

相关问题