BDD:源於TDD而勝於TDD
阿新 • • 發佈:2018-12-24
什麼是BDD?
Behave Driven Development(行為驅動開發?)
為什麼要有BDD?
BDD告訴我們我們要寫spec而不是Test,我要做的事是“Specification,not Verification”。
說了這麼多,BDD到底什麼樣子?BDD開發需要一個Behaviour Specification Framework,就像TDD需要xUnit,這裡的例子來自RSpec,一個如不要的Behaviour Specification Framework。(I love ruby!)
context "BDD framework"do
setup do
@bdd_framework = BddFramework.new
end
specify "should be adopted quickly"do
@bdd_framework.should_be_adopted_quickly
end
specify "should be intuitive"do
@bdd_framework.should_be_intuitive
end
end
怎麼樣?看了有什麼感想?
RSpec還有一個rails的外掛,可以和rails良好協作,例子程式碼:
context "A user (in general)"do
setup do
@user = User.new
end
specify "should be invalid without a username"do
@user.email ='[email protected]'
@user.password ='abcdefg'
@user.should_not_be_valid
@user.errors.on(:username).should_equal "is required"
@user.username ='someusername'
@user.should_be_valid
end
specify "should be invalid without an email"do
@user.username ='joebloggs'
@user.password ='abcdefg'
@user.should_not_be_valid
@user.errors.on(:email).should_equal "is required"
@user.email ='[email protected]'
@user.should_be_valid
end
specify "should be invalid without a password"do
@user.email ='[email protected]'
@user.username ='joebloggs'
@user.should_not_be_valid
@user.password ='abcdefg'
@user.should_be_valid
end
end 萬惡的csdn來個ruby語法加亮都沒有。
參考網址:http://behaviour-driven.org/
http://rspec.rubyforge.org/
http://www.lukeredpath.co.uk/2006/8/29/developing-a-rails-model-using-bdd-and-rspec-part-1
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!)
setup do
@bdd_framework = BddFramework.new
end
specify "should be adopted quickly"do
@bdd_framework.should_be_adopted_quickly
end
specify "should be intuitive"do
@bdd_framework.should_be_intuitive
end
end
怎麼樣?看了有什麼感想?
RSpec還有一個rails的外掛,可以和rails良好協作,例子程式碼:
context
setup do
@user = User.new
end
specify "should be invalid without a username"do
@user.email ='[email protected]'
@user.password ='abcdefg'
@user.should_not_be_valid
@user.errors.on(:username).should_equal "is required"
@user.username ='someusername'
@user.should_be_valid
specify "should be invalid without an email"do
@user.username ='joebloggs'
@user.password ='abcdefg'
@user.should_not_be_valid
@user.errors.on(:email).should_equal "is required"
@user.email ='[email protected]'
@user.should_be_valid
end
specify "should be invalid without a password"do
@user.email ='[email protected]'
@user.username ='joebloggs'
@user.should_not_be_valid
@user.password ='abcdefg'
@user.should_be_valid
end
end 萬惡的csdn來個ruby語法加亮都沒有。
參考網址:http://behaviour-driven.org/
http://rspec.rubyforge.org/
http://www.lukeredpath.co.uk/2006/8/29/developing-a-rails-model-using-bdd-and-rspec-part-1