Test-driven development Advanced Software Engineering 603 492 Dr Nuha El-Khalili
Test-driven development Design Code Test TDD isn’t about testing... ...it’s about programming! Traditional development cycle Test- driven development cycle Test Code Design
Test-driven development Create automated unit tests that define code requirements before writing the code itself Tests contain assertions that are either true or false. It makes the developer focus on the requirements before writing the code
Test-driven development
Test-driven development Always start with a failing test Quickly write the simplest code needed to pass the test Remove duplication or clean up (Refactor) Repeat as needed to meet requirements Test everything that could possibly break
What can be tested? Valid inputs Invalid inputs Errors, exceptions, and events Boundary conditions Everything that could possibly break!
Benefits Code is written for testability Ensures that tests for every feature will be written Implement code in small steps Revert code to the last version that passed all tests Better code, in less time, with no defects
Benefits More modularized code with loose coupling More maintainable code! Replace neighbors with mocks
But You still need integration testing and acceptance testing. Badly written test cases will waste the effort of TDD
TDD vs Unit testing A Unit Test is a test of a small functional piece of code Programmers do unit testing, but: Not structured Not Repeatable Not on all your code
Demo Download “test driven” http://www.testdriven.net/ Supports: Unit Testing Add-In for Microsoft Visual Studio Supports: Nunit Ncover
Demo Steps Open new project Go to the solution explorer and Add a project to the existing solution called Exampletest that has the "class library" type Go to Add references: open a dialog, go to browse tab go to Nunit folder and choose the units folder and choose the dll file inside that folder In the ExampleTest class add "import Nunit.framework"
Demo Steps Now create another normal class and add a function there that adds two numbers, do not implement the function. Go back to the exampleTest class and add a reference to the project Add "import ProjectName“ Create a test Create a method call Assert.areequal (10, add(4,6))
Demo Steps Right click on the test and choose run. Test results are down at the bottom window Test should fail Implement the function and re-test
See it on YouTube http://www.youtube.com/watch?v=SO_Pl7Khiw4&feature=related http://www.youtube.com/watch?v=WuNEnxL9CHU http://www.youtube.com/watch?v=9Mx7RELpW-4