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. 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. 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. 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
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. 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"
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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Voorbeelduitkomst - next steps
➢ Elke individuele regel naar een aparte methode
abstraheren
➢ De complete lijst laten zien met het uitvoeren van een
bestand
➢ User Interface