Embracing the Messiness in Search of Epic Solutions

Spock: Reading Test Data from CSV File

Posted

in

Following up on my recent post about creating a Spock specification to read the test data from a CSV file without loading all the data into the memory, I created a CSVReader that implements Iterable that allows me to pull this off. You may download the source code here.

With this implementation, I can now write an elegant Spock specification:-

class MySpockSpec extends Specification {
    @Unroll
    def "#firstNum + 1 == #secondNum"() {
        expect:
        Integer.valueOf(firstNum as String) + 1 == Integer.valueOf(secondNum as String)

        where:
        [firstNum, secondNum] << new CSVReader(getClass().getClassLoader().getResourceAsStream("test.csv"))
    }
}

Tags:

Comments

One response to “Spock: Reading Test Data from CSV File”

  1. Harish Avatar
    Harish

    It would be good if there was a section in the csv file for each Spec.testcase .That way there will be only one file for the entire project

Leave a Reply