java - 使用 Spock (groovy) 数据表测试没有参数的方法
问题描述
假设我要测试的方法是:
private void deleteImages() {
//iterate files in path
//if file == image then delete
}
现在要使用带有 spock 框架的 groovy 进行测试,我正在制作 2 个文件,并调用该方法:
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()
这按预期工作。
但是,我想在此测试中添加更多文件(例如:更多图像文件扩展名、视频文件扩展名等),因此使用数据表会更容易阅读。
如何将其转换为数据表?请注意,我的测试方法不带任何参数(目录路径是通过另一个服务模拟的,为简单起见,我没有在此处添加)。
我看到的所有数据表示例都是基于将输入更改为单个方法,但在我的情况下,设置有所不同,而该方法不接受任何输入。
理想情况下,设置完成后,我希望看到这样的表格:
where:
imageFileJPG.exists() | false
imageFileTIF.exists() | false
imageFilePNG.exists() | false
videoFileMP4.exists() | true
videoFileMOV.exists() | true
videoFileMKV.exists() | true
解决方案
如果你想使用数据表,你应该把 DATA 放在那里而不是方法调用。
因此,测试可能如下所示:
@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
.....
}
推荐阅读
- linux - Doom Emacs 中 Noto Color Emoji 和阿拉伯语之间的字体冲突
- linux - 为什么 cygwin putty cmd.exe 回滚缓冲区在 win10 上不随 GNU Screen 增长,但在 win7 上增长?
- typescript - 推断的泛型返回类型接受具有未知属性的对象
- r - 从摘要图中创建生存曲线
- python - Python split函数在while循环中不起作用
- php - Laravel - 404 | 在“/{id}”条目中未找到
- asp.net - .NET Core 中的隐式编译常量
- python - 合成功能
- java - Kafka ssl 连接“应提供 EventLoopGroup 和 ChannelType 或都不提供”
- python - 如何在 python 中使用 SQL 命令发送值?