Make assertions on a list in spock
Asked Answered
P

2

5

I have written a spock test where I'm trying to make assertions on a list of items. Let's say as an example I want to check every number in a list if it is equal to 500:

def numbers = [1,2,3,4]
numbers.each{
    assert it == 500
}

Returns the following

Assertion failed:

assert it == 500
       |  |
       1  false

How can I make an assertion on a list without stopping at the first failure? Here is the spock test to accompany my assertion:

def "Check if each number in a list is 500"{
given: "A list of numbers"
def numbers = [1,2,3,4]

expect: "each number to be 500"
numbers.each{
    assert it == 500
}

To clarify I want to see each failure in my spock test report

So I should see the 1 != 500, 2!= 500, etc.

Pops answered 27/9, 2016 at 15:45 Comment(0)
E
6

You can also have something like this:

@Unroll
def "Check if #number is 500"(){ 
    expect:  
        number == 500 
    where:
        number << [1,2,3,4] 
}

Not sure that fits your needs though

Estrella answered 27/9, 2016 at 16:4 Comment(4)
This is just a matter of style really, but when I @Unroll, I like to include something about each individual test case in the test name, just for documentation purposes if nothing else. So I would probably name the test "Check if #number is 500" instead of the name you chose.Conscious
I have updated the solution with your idea @BalRog as I also think that would even be a much better test nameMundell
This is what I want, but doing number << someList where someList is a list defined in a when block, it doesn't recognize someList. Any idea why? @BalRog @FranGarciaPops
The where block is evaluated before the when block, in fact, before anything else. Think of it as being like a sort of super-given block. So, you can set up static variables and use them in the where block, but you can't set up local variable in any of the other blocks and reference them in the where block.Conscious
C
2

If for some reason you cannot run as an @Unrolled test with a where clause, you might get an acceptable result from something like this:

def "Check if each number in a list is 500" () {
    given: "A list of numbers"
    def numbers = [1,2,3,4]

    expect: "each number to be 500"
    numbers.collect { it == 500 }.every { it }
}

When I ran it I got the following result:

Condition not satisfied:

numbers.collect { it == 500 }.every { it }
|       |                     |
|       |                     false
|       [false, false, false, false]
[1, 2, 3, 4]

    at WhateverSpec.Check if each number in a list is 500(WhateverSpec.groovy:10)

Notice that the second part of the condition expression shows that your equality expression is false for every item in the list. The third part evaluates the list of booleans assuring that they are all true (which of course, they aren't, ensuring your test fails without having to explicitly assert anything).

Conscious answered 27/9, 2016 at 22:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.