C16 Effective Unit Testing
C16 Effective Unit Testing
Win-Dev 2004
Friday, October 29, 2004
Peter Provost
Software Design Engineer
patterns & practices
Microsoft Corporation
Agenda
What is Unit Testing?
Typical Unit Testing Problems
Best Practices for Effective Unit Testing
Tools Demo
Q&A
What is Unit Testing?
A Definition
Unit Testing is code
that…
Is written by developers,
for developers.
Exercises a small,
specific area of
functionality.
Helps “prove” that a
piece of code does what
the developer expects it
to do.
What Unit Testing Is Not
Acceptance Testing (aka Functional Testing)
Performance Testing
Scalability Testing
Marick’s Four Quadrants of Testing
Business Facing
Critique Result
Scalability
Programmer Performance
Tests Usability
Security
Technology Facing
Test-first
Intuitive “poking and prodding” style testing
Smoke testing
Whatever the developer wants
Inconsistent Testing
Are tests required before check in?
How is it enforced?
Low Test Quality
Single biggest problem in unit testing today
Testing requires a special mindset
Tools only solve half the problem
Code Not Designed for Test
Often the cause of low test quality
Hard to design testable code
However, testable code often is better designed
Tests Not Maintained
So you’ve shipped your code and someone
finds a bug…
Do you fix the tests? Or just fix the bug?
What about the rest of the team?
Best Practices for Effective Unit
Testing
Automate Your Tests
If it is hard to run tests, you won’t do it
Manual (aka scripted) tests make regression
testing very hard
Use Good Tools
The tools should make it…
Easy to write tests
Easy to organize your tests
Easy to run tests (all, some, one)
Source: Lessons Learned in Software Testing, by Cem Kaner, James Bach, Brett
Pettichord
What to Test – BICEP
B Are the boundary conditions correct?
Source:http://c2.com/cgi/wiki?
MockObjects
Mock Downsides
Code complexity of being able to switch
between real and mock implementations
Maintaining the mock
Properties of Good Tests
Automatic Tests need to be run checked
automatically
Thorough Test everything that could
possibly break
Repeatable Tests should produce the same
results each time they are run
Independent A test should exercise only one
thing at a time
Professional Tests are code too! Write them
professionally.
Source: Pragmatic Unit Testing in C# with NUnit by Andrew Hunt and Dave
Thomas
Test Organization Options
Tests in the VS Project being tested
Best for experiments, spikes, etc.
E-mail: peterpr@microsoft.com
Weblog: http://www.peterprovost.org/