Reputation: 1767
Assuming the method I am trying to test is:
private void deleteImages() {
//iterate files in path
//if file == image then delete
}
Now to test this using groovy with spock framework, I am making 2 files, and calling the method:
def "delete images"() {
given:
//create new folder and get path to "path"
File imageFile = new File(path, "image.jpg")
imageFile.createNewFile()
File textFile= new File(path, "text.txt")
textFile.createNewFile()
}
when:
myclass.deleteImages()
then:
!imageFile.exists()
textFile.exists()
This is working as expected.
However, I want to add more files to this test (eg: more image file extensions, video file extensions etc) and therefore using a data table would be easier to read.
How can I convert this to a data table? Note that my test method does not take any parameters (the directory path is mocked via another service, which I have not added here for simplicity).
All the data table examples I saw were based on varying the input to a single method, but in my case, the setup is what is different, while the method takes no inputs.
Ideally, after the setup, I would like to see a table like this:
where:
imageFileJPG.exists() | false
imageFileTIF.exists() | false
imageFilePNG.exists() | false
videoFileMP4.exists() | true
videoFileMOV.exists() | true
videoFileMKV.exists() | true
Upvotes: 0
Views: 381
Reputation: 20699
If you want to use a data table, you should put DATA in there instead of method calls.
So, the test could look like this:
@Unroll
def 'some test for #fileName and #result'() {
expect:
File f = new File( fileName )
myclass.deleteImages()
f.exists() == result
where:
fileName | result
'imageFile.JPG' | false
'imageFile.TIF' | false
'videoFile.MKV' | true
.....
}
Upvotes: 3