Igor
Igor

Reputation: 1464

How to get index being tested on where block using spock?

Is there any way to get the index being tested on Spock? I have a where block like this:

where:
column1 | column2
1 | 3
1 | 4
2 | 5
6 | 8

I want to know if it's possible to get the index being executed on my test. So If I'm running the first test (1 | 3) my index would be 0. If I'm running the third test (2 | 5) my index would be 2.

Is there any way to get this index inside my test?

Upvotes: 3

Views: 979

Answers (5)

crusy
crusy

Reputation: 1512

There is a default (now?): #iterationCount, see here. You can use it in your method's name like

@Unroll
def "testing entry #iterationCount from where block"() { 
  // ...
}

There is #featureName available as well, by the way.

Upvotes: 1

Ari Singh
Ari Singh

Reputation: 1296

Adding an index column can be done in a groovy way. Just add the following line to the end of the "where:" block. The list should have the same number of items as the rows of data

idx << [0,1,2,3]

or

idx << (0..3).collect()

Reference: Secton "Combining Data Tables, Data Pipes, and Variable Assignments" of http://spockframework.org/spock/docs/1.0/data_driven_testing.html

Upvotes: 0

railsdog
railsdog

Reputation: 1501

The trivial answer would be to add an index or case or some categorical variable that can be checked in the result section.

where:
    idx | column1 | column2
    0   | 1       | 3
    1   | 1       | 4
    2   | 2       | 5
    3   | 6       | 8

But I have to wonder if the where clause is being used to run what maybe should be multiple test cases under the guise of a single test.

The idx shouldn't be telling the test code what checks to execute in the expect/then block, and shouldn't be driving any code that is being tested.

If the test yields different results if the order of the inputs was:

where:
    idx | column1 | column2
    0   | 6       | 8
    1   | 2       | 5
    2   | 1       | 4
    3   | 1       | 3

then I think the test needs to be broken up, because order sensitivity would seem to indicate that this test is testing something other than just the pairs of column1 and column2 values, and using where isn't exactly appropriate.

Upvotes: 1

dmahapatro
dmahapatro

Reputation: 50245

Something like below would suffice?

import spock.lang.*

@Unroll
class MyFirstSpec extends Specification {

  def "column1 is #column1 and column2 is #column2 where index is #index"() {
    expect:
    if(index in [0, 1]) {
        assert column1 < column2
    } else {
        assert column1 == column2
    }

    where:
    [column1, column2, index] << [
        [1, 2, 4, 5], [2, 3, 4, 5]
    ].transpose().withIndex()*.flatten()    
  }
}

Run this Sample

Note:
withIndex() is available from Groovy 2.4.0

Upvotes: 0

Matt Busche
Matt Busche

Reputation: 14333

You need to add, @Unroll to your test definition and then in the test name put #column1 to output the value

@Unroll
def "something #column1 and #column2"() { 
  ...
 }

Upvotes: 0

Related Questions