我有2种测试方法.
它们都执行where块的每一行,我需要清理添加和放松方法.
我已经尝试过cleanup block,void cleanup(),def cleanupSpec(),非套装.
如何在具有"where:"块的特定方法之后显式运行清理?
def "Add"() {
setup :
expect :
where:
}
def "Relax"() {
setup :
expect :
where:
}
Run Code Online (Sandbox Code Playgroud)
th3*_*org 13
您可以在方法中使用清理块,如下所示:
@Unroll
def "a method that tests stuff"(){
given:
def foo = fooDAO.save(new Foo(name: name))
when:
def returned = fooDAO.get(foo.id)
then:
returned.properties == foo.properties
cleanup:
fooDAO.delete(foo.id)
where:
name << ['one', 'two']
}
Run Code Online (Sandbox Code Playgroud)
每次测试迭代时,"清理"块将运行一次.
小智 5
如果您使用@Unroll, thencleanup:将为块中的每个条目调用where:。仅运行一次清理,然后将代码移至def cleanupSpec()闭包内。
@Shared
def arrayOfIds = []
@Unroll
def "a method that tests stuff"(){
given:
def foo = fooDAO.save(new Foo(name: name))
when:
def returned = fooDAO.get(foo.id)
arrayOfIds << foo.id
then:
returned.properties == foo.properties
where:
name << ['one', 'two']
}
def cleanupSpec() {
arrayOfIds.each {
fooDAO.delete(it)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12535 次 |
| 最近记录: |