Behave Driven Development(行为驱动开发?)
为什么要有BDD?
- TDD其实是测试行为的
- TDD的测试用例太专业,业务和需求人员看不懂
- TDD不知道哪些代码需要写测试,怎么写?
- 我们要写“规范(specifications)”,所有人都可以读懂,甚至业务人员可以帮你写
- 这些“规范”中的每一个都有明确的业务价值
BDD告诉我们我们要写spec而不是Test,我要做的事是“Specification,not Verification”。
说了这么多,BDD到底什么样子?BDD开发需要一个BehavIoUr Specification Framework,就像TDD需要xUnit,这里的例子来自RSpec,一个如不要的BehavIoUr Specification Framework。(I love ruby!)
context
"
BDDframework
do
setup
@bdd_framework = BddFramework. new
end
specify shouldbeadoptedquickly
@bdd_framework.should_be_adopted_quickly
end
specify shouldbeintuitive
@bdd_framework.should_be_intuitive
end
end
setup
@bdd_framework = BddFramework. new
end
specify shouldbeadoptedquickly
@bdd_framework.should_be_adopted_quickly
end
specify shouldbeintuitive
@bdd_framework.should_be_intuitive
end
end
RSpec还有一个rails的插件,可以和rails良好协作,例子代码:
Auser(ingeneral)
setup
@user User. shouldbeinvalidwithoutausername
@user.email ' joe@bloggs.com
@user.password abcdefg
@user.should_not_be_valid
@user.errors.on(:username).should_equal isrequired
@user.username someusername
@user.should_be_valid
end
specify shouldbeinvalidwithoutanemail joebloggs
@user.should_not_be_valid
@user.errors.on(:email).should_equal shouldbeinvalidwithoutapassword
@user.should_not_be_valid
@user.password
@user.should_be_valid
end
end
setup
@user User. shouldbeinvalidwithoutausername
@user.email ' joe@bloggs.com
@user.password abcdefg
@user.should_not_be_valid
@user.errors.on(:username).should_equal isrequired
@user.username someusername
@user.should_be_valid
end
specify shouldbeinvalidwithoutanemail joebloggs
@user.should_not_be_valid
@user.errors.on(:email).should_equal shouldbeinvalidwithoutapassword
@user.should_not_be_valid
@user.password
@user.should_be_valid
end
end