Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Intro to TDD and BDD


Published on

An introduction to Test Driven Development and Behavior Driven Development

Published in: Technology
  • Be the first to comment

Intro to TDD and BDD

  1. 1. Test Driven DesignBehavior Driven Design Jason Noble
  2. 2. Test Driven Design• TDD is a software development practice that relies on the repetition of a very short development cycle – Write a failing test that defines a desired improvement or new function – Write the minimum code possible to make the test pass – Refactor the code to acceptable standards
  3. 3. Red Green Refactor• Write a failing test (Red)• Make the test pass (Green)• Refactor tests or code (NOT both!)
  4. 4. Why use TDD?• Helps you break problems down into small manageable tasks• Writing tests takes the fear out of programming• Writing tests helps you communicate what your code SHOULD do/accomplish
  5. 5. TDD Example• What can a calculator do? – Add – Square Root – Subtract – On / Off – Memory Clear – Multiply – Memory Set – Divide – Memory Recall – Equals – Negative numbers – Percent – Decimals
  6. 6. Write a failing test (Red)• Write a failing test # spec/calculator_spec.rb require ’./calculator’ describe Calculator do end• Failure: spec/calculator_spec.rb:1:in `require: cannot load such file – ./calculator (LoadError)
  7. 7. Make the test pass (Green)• Write code to make the test pass touch calculator.rb• Test Failure spec/calculator_spec.rb:3:in `<top (required)>: uninitializedconstant Calculator (NameError)
  8. 8. Make the test pass (Green)• Write code to make the test pass # calculator.rb class Calculator end• Test Success No examples found.Finished in 0.00005 seconds0 examples, 0 failures
  9. 9. What did we do?• Write a very simple test• Got a LoadError• Fixed the LoadError• Got a NameError• Fixed the NameError• Tests are green!• Refactor?
  10. 10. Add a test # spec/calculator_spec.rb let(:calculator) { } it "can be created" do calculator.should be_a(Calculator) endTests are still green! Running: spec/calculator_spec.rb Calculator can be created Finished in 0.00127 seconds 1 example, 0 failures
  11. 11. What did we do?• Add a very simple test• Got no errors• Tests are green!• Refactor?
  12. 12. Add a test # spec/calculator_spec.rb describe "#total" do it "returns 0 by default" do == 0 end endTests are red!
  13. 13. Make the tests pass• Add the minimum code to make the test fail in a different way (or succeed) # calculator.rb def total end
  14. 14. Make the tests pass• Write the MINIMUM code necessary to make the test pass # calculator.rb def total 0 end• Tests are GREEN!• Refactor?
  15. 15. What did we do?• Write a very simple test (total.should == 0)• Got a NoMethod error• Fixed the NoMethodError• Got an unexpected result returned• Fixed the result to always return 0• Tests are green!• Refactor?
  16. 16. Behavior Driven Development• Uses the basis of TDD, domain driven design and object oriented design to provide software developers and business analysts with shared tools in order to collaborate on software development• TDD describes how the software works• BDD describes how the end user uses the software
  17. 17. BDD ExampleScenario: User adds two positive numbersGiven I turn the calculator on,Then I should see zero on the screenWhen I add 5And I add 7Then I should see 12 on the screen
  18. 18. Ruby Cucumber• Uses regular expressions to match steps Given /I turn the calculator on/ do @calculator = @calculator.on end Then /I should see (d+) on the screen/ do |expected_value| == expected_value end When /I add (d+) / do |number| @calculator.add(number) end
  19. 19. Further Readings• Test Driven Development: By Example by Kent Beck• Everyday Rails Testing with Rspec by Aaron Sumner• The Cucumber Book by Matt Wynne and Aslak Hellesoy