BDD:源于TDD而胜于TDD

来源:互联网 发布:湖南特大网络传销案 编辑:程序博客网 时间:2024/05/21 19:31
什么是BDD?
    Behave Driven Development(行为驱动开发?)
为什么要有BDD?
  1.     TDD其实是测试行为的
  2.     TDD的测试用例太专业,业务和需求人员看不懂
  3.     TDD不知道哪些代码需要写测试,怎么写?
  4.     我们要写“规范(specifications)”,所有人都可以读懂,甚至业务人员可以帮你写
  5.     这些“规范”中的每一个都有明确的业务价值
 BDD想要做到开发,需求,测试等人员不光是在开发相同的软件而且要使用同样的语言,什么语言?Specification,我这里翻译为规范。
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 
= 'joe@bloggs.com'
    @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 
= 'joe@bloggs.com'
    @user.should_be_valid
  end

  specify 
"should be invalid without a password" do
    @user.email 
= 'joe@bloggs.com'
    @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
原创粉丝点击