© ALEXANDRE CUVA VERSION 2.00 Test Driven Design
Alexandre Cuva Agile Transition Coach, Scrum Master, Product Owner Management 3.0 & Certified Scrum Developer Trainer Event speaker Hermes Swiss Project Team Professional ITIL V3 “I coach teams and organization to become highly productive.” Agile Transition Coach
Why should developers write tests? Common responses: “leave testing to QA” “developers are too busy” “developers don’t know how to test” “We don’t have bugs” “developers are intimately familiar with the structure of the code and are not well-suited for testing it”
But “if developers don’t test. How do they know that they are producing quality software?” Tests are a tool to help developers take responsibility for quality Tests help making small steps and give immediate feedback Test help maintain focus on measurable outcome of coding – producing the code that accomplishes a concrete objective
Kent Beck say Good unit tests … Express intent, not implementation details Run fast (they have short setups, run times, and break downs) Run in isolation (reordering possible) Run in parallel Use data that makes them easy to read and to understand
Testing - 8 -
TDD - 9 -
TDD
How to do it Design: figure out what you want to do Test: write a test to express the design It should FAIL Implement: write the code Test: run the test It should PASS
Test Unit Framework Microsoft Test MSTestIncluded inside -VS2010 Professional -VS2008 Team System NUnitMost Used Open Source More upToDate than MSTest xUnitFork from NUnit Most Used Open Source ?
Visual Studio Have always a separate project for your test. Unit Test : xxxTest ATDD : xxxAcceptanceTest BDD : xxxSpec Integration : xxxIntegrationTest
Visual Studio On Visual Studio : Create a Project “Test Project” Add new Test Select “Basic Unit Test”
Test int expected = 4; // Header int result = Calculator.Add(2, 2); //Body Assert.AreEqual(expected, result); //Footer
FAIL
Implement public static int Add(int number1, int number2) { return number1 + number2; }
Test
Iterate
Try It !
Write once, run often Write tests once Keep them somewhere sensible Run frequently (one click) No human input Machine-parsable output
When should I use TDD Always Write tests for anything you feel that might break Design of production code should always be test- driven No need to write tests for APIs you don’t own …
Two fundamental TDD rules (Kent Beck) Never write a single line of code unless you have a failing automated test. Eliminate duplication
Refactoring improves design “Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure” Martin Fowler
Question ? 25
Books
27 (twitter) (blog) management30.commanagement30.com (book)