Test-driven development (TDD)

Test-Driven Development
Unit Testing
Bra enhetstest
Dokumenterar din design
Gör det lättare att refactorera
Förändringar i ett test ska inte förstöra andra test
Små isolerade test som testar en sak
Testa inte sådant som uppenbart fungerar
När ett test upprepar sig ska de ge samma resultat
De ska ha en tydlig namngivning och vara lätta att förstå
Mocka externa källor
När ska man testa?
Arrange, Act, Assert
https://www.nuget.org/packages/NUnit/
https://www.nuget.org/packages/NUnit.Runners/
TDD
Varför?
”Uncle Bob” Martin (https://sites.google.com/site/unclebobconsultingllc/):
We do TDD for one overriding reason and several less important reasons. The less important reasons are:
1. We spend less time debugging.
2. The tests act as accurate, precise, and unambiguous documentation at the lowest level of the system.
3. Writing tests first requires decoupling that other testing strategies do not; and we believe that such decoupling is beneficial.
Those are ancillary benefits of TDD; and they are debatable. There is, however, one benefit that, given certain conditions are met,
cannot be debated:
If you have a test suite that you trust so much that you are willing to deploy the system based solely on
system based solely on those tests passing; and if that test suite can be executed in seconds, or minutes,
seconds, or minutes, then you can quickly and easily clean the code without fear.
Övning - TDD
StringCalculator Kata
http://osherove.com/tdd-kata-1/
Beroende
Test Double
http://xunitpatterns.com/Test%20Double.html
System under test (SUT)
Depended-on component (DOC)
Övning - TDD interaktion
StringCalculator Kata Interactions
http://osherove.com/tdd-kata-2/
FakeItEasy
https://github.com/FakeItEasy/FakeItEasy
Övning - Fakes
Mål: Programmera, via TDD, logiken för katt-import/export:
1.
Givet att katt-datan läses in från XML, se till att det går att hämta en katt för ett visst ID. Dvs testa "hämta-på-ID", fake:a XMLläsning.
2.
Gör det möjligt att lägg till en ny katt (t ex från web-gränssnitt )i XML-datakällan, och att den samtidigt får ett pris satt (enligt TDDövningen för pris). Dvs testa att prissättning sker, och att katt-datan skickas till XML på korrekt sätt, men fake:a prisuträkning (som
ju redan är testad), och den faktiska XML-hanteringen.
Övning – Conway’s Game of Life
https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
1) Any live cell with fewer than two live neighbors dies, as if caused by under-population.
2) Any live cell with two or three live neighbors lives on to the next generation.
3) Any live cell with more than three live neighbors dies, as if by overcrowding.
4) Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
Tutorial: http://jeremybytes.blogspot.se/2014/10/tdd-conways-game-of-life.html