spock測(cè)試框架,引用官方文檔的描述:
Spock is a testing and specification framework for Java and Groovy applications. What makes it stand out from the crowd is its beautiful and highly expressive specification language. Thanks to its JUnit runner, Spock is compatible with most IDEs, build tools, and continuous integration servers. Spock is inspired from JUnit, jMock, RSpec, Groovy, Scala, Vulcans, and other fascinating life forms.
這篇文章主要是簡(jiǎn)單介紹了spock的Data Driven Testing
數(shù)據(jù)啟動(dòng)測(cè)試:
通常谋梭,多次運(yùn)行相同的測(cè)試代碼,具有不同的輸入和預(yù)期結(jié)果是有用的
example:
class MathSpec extends Specification {
def "maximum of two numbers"() {
expect:
// exercise math method for a few different inputs
Math.max(1, 3) == 3
Math.max(7, 4) == 7
Math.max(0, 0) == 0
}
}
這個(gè)例子的寫(xiě)法簡(jiǎn)單庭呜,但是存在一些缺點(diǎn):
代碼和數(shù)據(jù)混合绍撞,不能輕易地獨(dú)立更改
- 數(shù)據(jù)不能自動(dòng)生成或從外部來(lái)源獲取
- 為了多次運(yùn)行相同的代碼眨八,必須將其復(fù)制或提取為單獨(dú)的方法
- 如果發(fā)生故障堂氯,可能不會(huì)立即清楚哪些輸入導(dǎo)致故障
- 多次執(zhí)行相同的代碼不會(huì)像執(zhí)行單獨(dú)的方法那樣受益于相同的隔離
Spock的數(shù)據(jù)驅(qū)動(dòng)測(cè)試
數(shù)據(jù)測(cè)試驅(qū)動(dòng)解決了這個(gè)問(wèn)題蔑担,首先引入了三個(gè)數(shù)據(jù)變量
class MathSpec extends Specification {
def "maximum of two numbers"(int a, int b, int c) {
expect:
Math.max(a, b) == c
...
}
}
在完成邏輯的測(cè)試部分,我們還需要提供相應(yīng)的數(shù)據(jù)咽白,這些工作則是在 where:
保存相應(yīng)的數(shù)據(jù)塊啤握。
databales
固定數(shù)據(jù)集,
class MathSpec extends Specification {
def "maximum of two numbers"(int a, int b, int c) {
expect:
Math.max(a, b) == c
where:
a | b | c
1 | 3 | 3
7 | 4 | 7
0 | 0 | 0
}
}
表的第一行聲明了數(shù)據(jù)變量晶框,表的后續(xù)行排抬,保存了相應(yīng)的值。對(duì)于其中的每一行授段,feature method
將都執(zhí)行一次蹲蒲,稱之為方法的迭代。
Tips
數(shù)據(jù)表必須至少有兩列畴蒲。單列表可以寫(xiě)成:
where:
a | _
1 | _
7 | _
0 | _
Method Unrolling
@Unroll
spock里的一個(gè)注解:
A method annotated with @Unroll will have its iterations reported independently:
有@Unroll的方法對(duì)每一個(gè)迭代都有自己的獨(dú)立報(bào)告悠鞍。
example:
@Unroll
def "maximum of two numbers"() {
...
}
maximum of two numbers[0] PASSED
maximum of two numbers[1] FAILED
Math.max(a, b) == c
| | | | |
| 7 4 | 7
42 false
maximum of two numbers[2] PASSED
This tells us that the second iteration (with index 1) failed.
資料來(lái)源:spock doc:
http://spockframework.org/spock/docs/1.1/data_driven_testing.html
作者水平有限对室,出現(xiàn)錯(cuò)誤請(qǐng)您指正模燥,謝謝。