Unit Testing

Preventing Bugs with Unit Testing
Chapter 5 - Coder to Developer by Mike Gunderloy
Instructor : Dr.James Fawcett
Presented by
Priyaa Nachimuthu
[email protected]
Agenda
 Types of Testing
 Testing for Developers
 Unit Testing
 Using NUnit
 Test – Driven Development
 Refactoring
 Unit Testing Summary
Types of Testing
 Unit Testing – testing very small pieces of code
 Integration Testing - code doesn't break someone else’s code
 System Testing – exercising the entire application to make it fail
 Stress Testing – push the limits of the application until it fails
 Beta Testing – giving copies of the unfinished code to outside
testers for feedback
 Acceptance Testing – formal verification of requirements
Testing for Developers
 Check out the module
 Write the code for the next feature to be implemented
 Write unit tests to test the feature
 Ensure that the code passes the unit tests
 Get the latest code for the entire application
 Make sure the whole project passes the integration tests
 Check in code and unit tests
Unit Testing

JUnit – by Erich Gamma and Kent Beck for Java development

NUnit – free, open source framework, a .Net unit testing
tool.NUnitAddin,TestRunner for NUnit integrates NUnit with VS.Net.NUnitAsp
supports NUnit testing of ASP. Net pages. ( http://nunit.org )

csUnit – free, open-source implementation. Additional features, includes project
wizards to build new testing projects easily and a UI with more information. (
http://csunit.org )

HarnessIt - $199.Pluaggable test engine architecture. Easy testing of web and
remote applications using the same tools as local windows applications.(
www.unittesting.com/default.aspx )

X-Unity - $129.VS.Net integration, a development kit for unit tests, and
integration ( http://x-unity.miik.com.ua/ )

.TEST - $900.Writes many unit tests for the user. Uses rules-based engine to
ensure code correctness and allows the user to write custom tests.(
www.parasoft.com/jsp/products/home.jsp?product=TestNet )
Using NUnit for Unit Testing
 A simple test
 Debugging NUnit tests
 Testing an exception
 Testing with mock objects
Test Driven Development
 A new testing technique by advocates of Extreme
Programming.
 Introduction to TDD : “Test-Driven Development”
by Kent Beck.
 Two simple rules
- Write a failing automated test before writing
any code
- Remove duplication
TDD – Plan of Action
 Quickly add a test
 Run all tests and see the new one fail
 Make a little change
 Run all tests and see them all succeed
 Refactor to remove duplication
TDD in Action
 Code Sample
Refactoring
 Martin Fowler’s Refactoring : Improving the Design of
the existing Code
 The process of changing a software system in such a
way that it doesn't alter the external behavior of the
code yet improves its internal structure.
 Refactoring patterns
 Code Thrashing – developers with different ideas
Refactoring Tools
 dotEasy – free ( www.doteasy.com.uy/ )
 The C# Refactoring Tool - $110 (
http://dotnetrefactoring.com/ )
 C# Refactory - $99 ( www.xtreme-
simplicity.net/CSharpReFactory.html )
 FlyWheel - $449 ( www.velocitis.com/ )
Unit Testing Checklist
 Write unit tests for all code
 Don’t postpone the tests
 Ensure that the code passes unit tests and integration tests
before check-ins
 Use automated tools such as NUnit
 Check the tests and any other setup files into SCC
 Consider using TDD for 100% coverage
 Refactor code only when there are thorough set of tests for the
code
Thank you!