TDD的进化
JUnit之后,陆续的,各种语言也都有了自己的Unit Test Framework,一时间TDD工具如雨后春笋,拔地而起。 然而,在跟风儿的大多数们熟练工具的时候,追寻更敏捷的前辈们继续向前进发了。TDD的下一步是哪里呢?更好的隔离代码(Working Effectively with legacy code),更好的测试构架(Xunit Test Patterns),这些都是锦上添花。但是,雪中送炭的出路在哪里呢?前辈们绞尽脑汁,想出两条路:
● 现有的TDD中的T,即测试用例还是太底层,到了集成层,系统层,用户层,再这么走,走不通。所以前辈们想出了另一条路,验收性测试驱动开发(Automate Acceptance Test Driven Development,AATDD),这里头比较出名的工具属Fitnesse。
● 现有的测试用例,还是不够易懂,毕竟是代码,最好让它更接近自然语言。
BDD的由来
行为驱动开发(Behavior Driven development,BDD),就是从第二个点子里演变而来的。继续上述例子,我希望让我的测试代码更易懂,我可以使用这样的方式:
2 |
def test_point_should_support_set_integer_to_x_and_y |
4 |
def test_invalid_point -> |
5 |
def test_point_should_raise_error_when_set_none_integer_to_x_and_y | |
如果使用Ruby的BDD的工具Rspec,则可以将测试代码以自然语言的方式描述为:
describe "A new point" do |
02 |
it "should raise exception when set none integer as x or y" do |
03 |
none_integers = [[ nil , 3 ], [ 3 , nil ], [ 1 , - 2 ], [- 2 , 1 ], [ 1 . 5 , 2 ], [ 2 , 1 . 5 ]] |
04 |
none_integers. each do |ni| |
05 |
Point. new (ni[ 0 ], ni[ 1 ]).should raise_error() |
08 |
it "should accept when set integer as x and y" do |
09 |
integers = [[ 1 , 2 ], [ 0 , 0 ]] |
11 |
p = Point. new (i[ 0 ], i[ 1 ]) |
|
Rspec后,其他编程语言也纷纷效仿,一系列Spec工具又如雨后春笋般。但是,追寻更敏捷的前辈们,又没有停下脚步,因为Rspec依旧停留在单元测试层,即describe/it/do的里面,还是代码。为了将自然语言引入Spec,,前辈们在此基础上开发了Cucumber。