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.

Test Driven Development Workshop

756 views

Published on

This presentation was used for a workshop on Test Driven Development, using the Fizz Buzz Coding Dojo Kata.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Test Driven Development Workshop

  1. 1. Test Driven Development
  2. 2. Introductie ● Wat is TDD? ● Waarom TDD? ● Workshop
  3. 3. Wat is TDD? Wikipedia: a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards
  4. 4. Wat is TDD? Wikipedia: a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards
  5. 5. Wat is TDD? Wikipedia: a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards
  6. 6. Wat is TDD? Wikipedia: a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards
  7. 7. Waarom TDD NIET testen:
  8. 8. Waarom TDD
  9. 9. De workshop - Coding Dojo regels Groepjes van twee Computer wisselt met elke stap Alle code moet getest zijn Er zijn meerdere oplossingen mogelijk Probeer eens iets nieuws
  10. 10. De opdracht Toon een opeenvolgende rij van getallen Als een getal deelbaar is door 3, toon "Fizz" Als een getal deelbaar is door 5, toon "Buzz" Als een getal deelbaar is door 3 en 5, toon "FizzBuzz"
  11. 11. Voorbeelduitkomst "1" "11" "Fizz" "31" "41" "2" "Fizz" "22" "32" "Fizz" "Fizz" "13" "23" "Fizz" "43" "4" "14" "Fizz" "34" "44" "Buzz" "FizzBuzz" "Buzz" "Buzz" "FizzBuzz" "Fizz" "16" "26" "Fizz" "46" "7" "17" "Fizz" "37" "47" "8" "Fizz" "28" "38" "Fizz" "Fizz" "19" "29" "Fizz" "49" "Buzz" "Buzz" "FizzBuzz" "Buzz" "Buzz"
  12. 12. Voorbeeldoplossing - stap 1 Schrijf een (falende) test: test_fizz_buzz.rb: require_relative "fizz_buzz" console: E require "test/unit" Finished tests in 0.000378s, 2642.4268 tests/s, 0.0000 assertions/s. class TestFizzBuzz < Test::Unit::TestCase 1) Error: test_number_3(TestFizzBuzz): NoMethodError: undefined method `print for FizzBuzz:Class test_fizz_buzz.rb:6:in `test_number_3' def test_number_3 assert_equal "Fizz", FizzBuzz.print(3) end end 1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
  13. 13. Voorbeeldoplossing - stap 2 Schrijf de minimale code om de test te laten lukken: fizz_buzz.rb: class FizzBuzz def self.print(number) return "Fizz" if number == 3 end end console: . Finished tests in 0.000306s, 3266.2660 tests/s, 3266.2660 assertions/s. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
  14. 14. Voorbeeldoplossing - stap 3 Wat gebeurt er als we 6 meegeven? test_fizz_buzz.rb: class TestFizzBuzz < Test::Unit::TestCase def test_number_3 assert_equal "Fizz", FizzBuzz.print(3) end def test_number_6 assert_equal "Fizz", FizzBuzz.print(6) end end console: .F Finished tests in 0.000525s, 3808.0804 tests/s, 3808.0804 assertions/s. 1) Failure: test_number_6(TestFizzBuzz) [test_fizz_buzz.rb: 10]: <"Fizz"> expected but was <nil>. 2 tests, 2 assertions, 1 failures, 0 errors, 0 skips
  15. 15. Voorbeeldoplossing - stap 4 Herschrijf de code: fizz_buzz.rb: class FizzBuzz def self.print(number) return "Fizz" if (number % 3 == 0) end end console: .. Finished tests in 0.000342s, 5851.4436 tests/s, 5851.4436 assertions/s. 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
  16. 16. Voorbeeldoplossing - stap 5 Schrijf de test voor nummer 5: test_fizz_buzz.rb: class TestFizzBuzz < Test::Unit::TestCase def test_number_5 assert_equal "Buzz", FizzBuzz.print(5) end end console: .F. Finished tests in 0.000589s, 5092.4794 tests/s, 5092.4794 assertions/s. 1) Failure: test_number_5(TestFizzBuzz) [test_fizz_buzz.rb: 14]: <"Buzz"> expected but was <nil>. 3 tests, 3 assertions, 1 failures, 0 errors, 0 skips
  17. 17. Voorbeeldoplossing - stap 6 Schrijf de code voor nummer 5: fizz_buzz.rb: class FizzBuzz def self.print(number) return "Fizz" if (number % 3 == 0) return "Buzz" if number == 5 end end console: ... Finished tests in 0.000352s, 8522.3157 tests/s, 8522.3157 assertions/s. 3 tests, 3 assertions, 0 failures, 0 errors, 0 skips
  18. 18. Voorbeeldoplossing - stap 7 Wat gebeurt er nu bij nummer 10? test_fizz_buzz.rb: class FizzBuzz < Test::Unit::TestCase def test_number_5 assert_equal "Buzz", FizzBuzz.print(5) end def test_number_10 assert_equal "Buzz", FizzBuzz.print(10) end end console: F... Finished tests in 0.000565s, 7083.0057 tests/s, 7083.0057 assertions/s. 1) Failure: test_number_10(TestFizzBuzz) [test_fizz_buzz.rb: 18]: <"Buzz"> expected but was <nil>. 4 tests, 4 assertions, 1 failures, 0 errors, 0 skips
  19. 19. Voorbeeldoplossing - stap 8 Herschrijf de code: fizz_buzz.rb: class FizzBuzz def self.print(number) return "Fizz" if (number % 3 == 0) return "Buzz" if (number % 5 == 0) end end console: .... Finished tests in 0.000387s, 10343.3474 tests/s, 10343.3474 assertions/s. 4 tests, 4 assertions, 0 failures, 0 errors, 0 skips
  20. 20. Voorbeeldoplossing - stap 9 Schrijf de test voor 3 en 5: test_fizz_buzz.rb: class TestFizzBuzz < Test::Unit::TestCase def test_number_15 assert_equal "FizzBuzz", FizzBuzz.print(15) end end console: .F... Finished tests in 0.000618s, 8085.1594 tests/s, 8085.1594 assertions/s. 1) Failure: test_number_15(TestFizzBuzz) [test_fizz_buzz.rb: 22]: <"FizzBuzz"> expected but was <"Fizz">. 5 tests, 5 assertions, 1 failures, 0 errors, 0 skips
  21. 21. Voorbeeldoplossing - stap 10 Schrijf de code voor 3 en 5: fizz_buzz.rb: class FizzBuzz def self.print(number) return "FizzBuzz" if number == 15 return "Fizz" if (number % 3 == 0) return "Buzz" if (number % 5 == 0) end end console: ..... Finished tests in 0.000392s, 12763.0138 tests/s, 12763.0138 assertions/s. 5 tests, 5 assertions, 0 failures, 0 errors, 0 skips
  22. 22. Voorbeeldoplossing - stap 11 Wat gebeurt er nu bij 30? test_fizz_buzz.rb: class TestFizzBuzz < Test::Unit::TestCase def test_number_15 assert_equal "FizzBuzz", FizzBuzz.print(15) end def test_number_30 assert_equal "FizzBuzz", FizzBuzz.print(30) console: ...F.. Finished tests in 0.000599s, 10010.2104 tests/s, 10010.2104 assertions/s. 1) Failure: test_number_30(TestFizzBuzz) [test_fizz_buzz.rb: 26]: <"FizzBuzz"> expected but was <"Fizz">. end end 6 tests, 6 assertions, 1 failures, 0 errors, 0 skips
  23. 23. Voorbeeldoplossing - stap 12 Schrijf de code voor 30: fizz_buzz.rb: class FizzBuzz def self.print(number) return "FizzBuzz" if (number % 3 == 0) && (number && 5 == 0) return "Fizz" if (number % 3 == 0) return "Buzz" if (number % 5 == 0) end end console: ...... Finished tests in 0.000423s, 14191.6775 tests/s, 14191.6775 assertions/s. 6 tests, 6 assertions, 0 failures, 0 errors, 0 skips
  24. 24. Voorbeeldoplossing - stap 13 Refactor de code voor 30: fizz_buzz.rb: class FizzBuzz def self.print(number) return "FizzBuzz" if (number % 15 == 0) return "Fizz" if (number % 3 == 0) return "Buzz" if (number % 5 == 0) end end console: ...... Finished tests in 0.000423s, 14191.6775 tests/s, 14191.6775 assertions/s. 6 tests, 6 assertions, 0 failures, 0 errors, 0 skips
  25. 25. Voorbeeldoplossing - stap 14 Wat als we een foutje hadden gemaakt? fizz_buzz.rb: console: F.F.FF class FizzBuzz Finished tests in 0.000999s, 6007.5034 tests/s, 6007.5034 assertions/s. def self.print(number) return "FizzBuzz" if (number % 3 == 0) || (number % 5 == 0) return "Fizz" if (number % 3 == 0) return "Buzz" if (number % 5 == 0) end end 1) Failure: test_number_10(TestFizzBuzz) [test_fizz_buzz.rb:18]: <"Buzz"> expected but was <"FizzBuzz">. 2) Failure: test_number_3(TestFizzBuzz) [test_fizz_buzz.rb:6]: <"Fizz"> expected but was <"FizzBuzz">. 3) Failure: test_number_5(TestFizzBuzz) [test_fizz_buzz.rb:14]: <"Buzz"> expected but was <"FizzBuzz">. 4) Failure: test_number_6(TestFizzBuzz) [test_fizz_buzz.rb:10]: <"Fizz"> expected but was <"FizzBuzz">. 6 tests, 6 assertions, 4 failures, 0 errors, 0 skips
  26. 26. Voorbeeldoplossing - stap 15 Schrijf de test voor alle andere getallen: test_fizz_buzz.rb: console: ..F.... class TestFizzBuzz < Test::Unit::TestCase Finished tests in 0.000631s, 11091.4634 tests/s, 11091.4634 assertions/s. def test_number_2 assert_equal "2", FizzBuzz.print(2) end 1) Failure: test_number_2(TestFizzBuzz) [test_fizz_buzz.rb:30]: <"2"> expected but was <nil>. 7 tests, 7 assertions, 1 failures, 0 errors, 0 skips end
  27. 27. Voorbeeldoplossing - stap 16 Schrijf de code voor alle andere getallen: fizz_buzz.rb: console: ....... class FizzBuzz Finished tests in 0.000520s, 13452.1220 tests/s, 13452.1220 assertions/s. def self.print(number) return "FizzBuzz" if (number % 15 == 0) return "Fizz" if (number % 3 == 0) return "Buzz" if (number % 5 == 0) return number.to_s end end 7 tests, 7 assertions, 0 failures, 0 errors, 0 skips
  28. 28. Voorbeelduitkomst - next steps ➢ Elke individuele regel naar een aparte methode abstraheren ➢ De complete lijst laten zien met het uitvoeren van een bestand ➢ User Interface
  29. 29. Voorbeeldoplossing - stap 17 Itereren over 100 getallen: fizz_buzz.rb: class FizzBuzz def self.print(number) return "FizzBuzz" if (number % 15 == 0) return "Fizz" if (number % 3 == 0) return "Buzz" if (number % 5 == 0) return number.to_s end end (1..100).each{|number| puts FizzBuzz.print(number)} console: ruby fizz_buzz.rb 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 ...etc

×