際際滷

際際滷Share a Scribd company logo
Coding Dojo - TDD Basics
Steve Zhang
Coding dojo - TDD
3 Laws of TDD
 First Law: You may not write production code
until you have written a failing unit test.
 Second Law: You may not write more of a
unit test than is sufficient to fail, and not
compiling
 Third Law: You may not write more
production code than is sufficient to pass the
currently
Clean Code, p122 ; Robert Martin
Arrange  Act -Assert
Fake It!
 Fake it till you make it
 Triangulation
As the tests get more specific, the code gets
more generic.
- Uncle Bob
Refactoring
 Change code without changing its behavior
 Refactoring != Redesign
 Remove the Fake
 Remove Duplication
 Refactor both test code and production code
F.I.R.S.T. Unit Tests
 Fast
 Independent
 Repeatable
 Self-validating
 Timely
Clean Code, pp 132-133 ; Robert Martin
Test Methods
 Single assert
 No conditional logic
 Standard format
 Setup
 Exercise
 Verify
 Teardown
Recommend Books
Lets Start!
 Kata: StringCalculator
 Test method Example
add_With_Empty_String_Returns_Zero()

More Related Content

Coding dojo - TDD

  • 1. Coding Dojo - TDD Basics Steve Zhang
  • 3. 3 Laws of TDD First Law: You may not write production code until you have written a failing unit test. Second Law: You may not write more of a unit test than is sufficient to fail, and not compiling Third Law: You may not write more production code than is sufficient to pass the currently Clean Code, p122 ; Robert Martin
  • 4. Arrange Act -Assert
  • 5. Fake It! Fake it till you make it Triangulation
  • 6. As the tests get more specific, the code gets more generic. - Uncle Bob
  • 7. Refactoring Change code without changing its behavior Refactoring != Redesign Remove the Fake Remove Duplication Refactor both test code and production code
  • 8. F.I.R.S.T. Unit Tests Fast Independent Repeatable Self-validating Timely Clean Code, pp 132-133 ; Robert Martin
  • 9. Test Methods Single assert No conditional logic Standard format Setup Exercise Verify Teardown
  • 11. Lets Start! Kata: StringCalculator Test method Example add_With_Empty_String_Returns_Zero()

Editor's Notes

  1. Arrange all the things needed to exercise the code Act on the code you want to verify Assert that the code worked as expected
  2. Robert Martin uncle bob OO design guru - S.O.L.I.D. principles One of 17 formulators of Agile manifesto. Manifesto his idea One founder of Craftsmanship movement. Manifesto for Software Craftsmanship. Fast Must be able to run quickly. <10 minutes ideal Independent No test should depend on another. Order should not matter. Repeatable Should be able to run them at any stage, development, QA, production Timely - Created with code
  3. Having a single assert is an ideal. The idea is that the test should test ONLY ONE THING. In practice you might find a couple of asserts are needed, but try to keep it to one.