Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing Spock-将集合上的断言扩展为单独的测试,如@Unroll_Unit Testing_Groovy_Spock - Fatal编程技术网

Unit testing Spock-将集合上的断言扩展为单独的测试,如@Unroll

Unit testing Spock-将集合上的断言扩展为单独的测试,如@Unroll,unit-testing,groovy,spock,Unit Testing,Groovy,Spock,在Spock中,是否有一种方法可以将集合中每个项的断言转换为自己的测试,该测试将通过/失败,类似于和@Unroll时发生的情况 如果我遵循最简单和最明显的方法(记录在中) 然后,测试失败将停止迭代,而不会测试列表中的其他项。我可以在循环中建立一个错误列表,然后将assert拉到循环之外,但我希望从测试框架中获得一些优势 我可以用where代替 then: list[idx].foo == bar where: idx || bar 0 || ... 1 || ... 2 ||

在Spock中,是否有一种方法可以将集合中每个项的断言转换为自己的测试,该测试将通过/失败,类似于
@Unroll
时发生的情况

如果我遵循最简单和最明显的方法(记录在中)

然后,测试失败将停止迭代,而不会测试列表中的其他项。我可以在循环中建立一个错误列表,然后将
assert
拉到循环之外,但我希望从测试框架中获得一些优势

我可以用
where
代替

then: list[idx].foo == bar
where: 
idx || bar 
0   || ...
1   ||  ...
2   ||  ...
....
但是,这会多次重新运行整个测试,这也不是我想要的——我想要一次测试执行,但是每个集合元素上的断言都报告为一个独立的测试通过/失败。

这是一次测试执行,在同一个输出上有多个测试

这个怎么样

when: 
def list = // do a bunch of stuff

then: 
list*.foo == [bar1, bar2, ...]
最后,它只是两个集合之间的断言。如果您正在检查所有
foo
s,则其值应与
bar
完全相同:

then:
list*.foo.every { it == bar }

它在Spock中也是
@Unroll


有关更多详细信息,请查看dock:

按照OlgaMaciaszek的建议,使用助手方法创建列表,然后使用
@Unroll
如何

class UnrolledSpec extends Specification {

    def bunchOfStuff() {
        println "Doing a bunch of stuff"
        return [-1, 1, 2, 3, 4, 5]
    }

    @Unroll
    def "That all numbers are greater than zero [#number]"() {
        expect:
        println "Testing ${number}"
        number > 0
        where:
        number << bunchOfStuff()
    }

}

这仍然会表现为单个测试失败-就度量而言,集合中的一个元素失败还是所有元素失败都是一样的。是的,我的观点是,当我使用
where
@Unroll
时,它会多次执行整个测试。我正在寻找一种方法来有选择地展开测试中的一些循环。我认为它不会多次执行测试。我认为这只会改变报告的表达方式。至少我是这样解释文档中的这句话的:“请注意,展开对方法的执行方式没有影响;它只是报告中的一种替代方法。”您可以在这里找到:。
@Unroll
其中
是两个不同的东西<代码>其中
是导致测试重复的原因
@Unroll
更改了结果的显示方式。我认为单凭斯波克提供的机制无法做到这一点。我认为最接近的方法是在
setupSpec()
子句中包含任何您不想重复的代码,然后只在测试中放入带有断言和
where
表的
expect:
子句;通过这种方式,测试将被迭代,但它将主要由断言组成,因此不会给您带来太多开销。问题是首先没有得到列表。这是
where
@Unroll
的行为-我不想多次执行整个测试。相反,我只想执行一次测试,但要对单个集合元素进行声明,以独立报告成功/失败。您将测试代码放在
bunchOfStuff()
中,该测试代码将执行一次,然后在
where
子句中有您的断言,该断言将单独执行和报告。
class UnrolledSpec extends Specification {

    def bunchOfStuff() {
        println "Doing a bunch of stuff"
        return [-1, 1, 2, 3, 4, 5]
    }

    @Unroll
    def "That all numbers are greater than zero [#number]"() {
        expect:
        println "Testing ${number}"
        number > 0
        where:
        number << bunchOfStuff()
    }

}
Doing a bunch of stuff
Testing -1

Condition not satisfied:

number > 0
|      |
-1     false

    at UnrolledSpec.That all numbers are greater than zero [#number](UnrolledSpec.groovy:20)

Testing 1
Testing 2
Testing 3
Testing 4
Testing 5