Download - Intro to TDD and BDD
![Page 2: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/2.jpg)
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
![Page 3: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/3.jpg)
Red Green Refactor
• Write a failing test (Red)
• Make the test pass (Green)
• Refactor tests or code (NOT both!)
![Page 4: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/4.jpg)
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
![Page 5: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/5.jpg)
TDD Example
• What can a calculator do?– Add– Subtract– Multiply– Divide– Equals– Percent
– Square Root– On / Off– Memory Clear– Memory Set– Memory Recall– Negative numbers– Decimals
![Page 6: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/6.jpg)
Write a failing test (Red)
• Write a failing test# spec/calculator_spec.rbrequire ’./calculator’
describe Calculator doend
• Failure:spec/calculator_spec.rb:1:in `require': cannot load such file – ./calculator (LoadError)
![Page 7: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/7.jpg)
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)>': uninitialized constant Calculator (NameError)
![Page 8: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/8.jpg)
Make the test pass (Green)
• Write code to make the test pass
# calculator.rbclass Calculatorend
• Test Success
No examples found.
Finished in 0.00005 seconds0 examples, 0 failures
![Page 9: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/9.jpg)
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?
![Page 10: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/10.jpg)
Add a test
# spec/calculator_spec.rb let(:calculator) { Calculator.new } it "can be created" do calculator.should be_a(Calculator) end
Tests are still green!Running: spec/calculator_spec.rb
Calculator can be created
Finished in 0.00127 seconds1 example, 0 failures
![Page 11: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/11.jpg)
What did we do?
• Add a very simple test• Got no errors• Tests are green!• Refactor?
![Page 12: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/12.jpg)
Add a test
# spec/calculator_spec.rb describe "#total" do it "returns 0 by default" do calculator.total.should == 0 end end
Tests are red!
![Page 13: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/13.jpg)
Make the tests pass
• Add the minimum code to make the test fail in a different way (or succeed)
# calculator.rbdef totalend
![Page 14: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/14.jpg)
Make the tests pass
• Write the MINIMUM code necessary to make the test pass
# calculator.rbdef total 0end
• Tests are GREEN!• Refactor?
![Page 15: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/15.jpg)
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?
![Page 16: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/16.jpg)
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
![Page 17: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/17.jpg)
BDD Example
Scenario: User adds two positive numbers
Given I turn the calculator on,Then I should see zero on the screen
When I add 5And I add 7Then I should see 12 on the screen
![Page 18: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/18.jpg)
Ruby Cucumber
• Uses regular expressions to match stepsGiven /I turn the calculator on/ do @calculator = Calculator.new @calculator.onendThen /I should see (\d+) on the screen/ do |expected_value|
@calculator.total.should == expected_valueendWhen /I add (\d+) / do |number|
@calculator.add(number)end
![Page 19: Intro to TDD and BDD](https://reader034.vdocuments.us/reader034/viewer/2022042607/5553ab2bb4c905d9448b4848/html5/thumbnails/19.jpg)
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