RSM
RSM

Reputation: 433

Read CSV file and use each line ias a input in where clause of spock

Below is the example of my CSV file

A1,B1
A2,B2
A3,B3

Here is my Spock test:

def testCSV() {
 when:
   def A = ValueOfA
   def B = ValueOfA

 then:  
  println A 
  println B

where:
 ValueOfA | ValueOfA
**get these value from csv file**

}

Is it possible to read the csv file and pass the value in the where clause?

Upvotes: 0

Views: 743

Answers (2)

Balajilinks
Balajilinks

Reputation: 33

def testCSV() {

 when:
   def A = inputValues.split(",")[0]
   def B = inputValues.split(",")[1]

 then:  
  println A 
  println B

where:

 inputValues << new File( "\\src\\test\\resources\\test.txt").readLines().toList()

/*  To get specific Rows
 inputValues << new File( "\\src\\test\\resources\\test.txt").readLines().toList().subList(1,10)
*/
}

Upvotes: 0

Fran Garc&#237;a
Fran Garc&#237;a

Reputation: 2050

You can read the csv file in a private method like this http://groovy-almanac.org/csv-parser-with-groovy-categories/ and then you can assign the variables a and b like this:

where:
a << [readCSVAMap[0],readCSVAMap[1],readCSVAMap[2]]
b << [readCSVBMap[0],readCSVBMap[1],readCSVBMap[2]]

Upvotes: 4

Related Questions