Thursday, June 19, 2008

Lesson Learned Unit Testing

Lesson #1: Write tests using the 3A pattern
Arrange - Set up the test harness
Act - Run the thing you actually want to test
Assert - Verify the results
Benefits
Readability
Consistency
Liabilities
More verbose
Might need to introduce local variables
Lesson #2: Keep your tests close
Tests should be as close as possible to the production code
Keep in Same assembly
Treat them like production code
Liabilities
Should you ship your tests?
Lesson #3: ExpectedException leads to uncertainty
Violates 3A
Record Excpetion Instead
Problem:Don’t know which line of code threw the exception. Test can pass for the wrong reason
Test For Exact Exception if doing this
Benefits
Readability
Identify and isolate the code you expect to throw
Liabilities
Act and Assert are together
Lesson #4: Small fixtures
Your code should be a book for people to understand
Break Unit tests up into easy to read section
Benefits
Smaller, more focused test classes
Liabilities
Potential code duplication
May consider duplicating code if it’s for the purpose of communication
Lesson #5: Don’t use SetUp or TearDown
You can end up with a test that doesn’t need anything from the SetUp method.
If SetUp takes a long time, you’d be paying the price for every test, even those that don’t need all of it
Put duplicated code into a method, and calling it at the beginning of each test
Benefits
Readability (if it removes duplication)
Test isolation
Liabilities
Duplicated initialization code
Lesson #6: Don’t use abstract base test classes
Shouldn't be a problem as VSTS doesn't allow them

Interesting Stuff hopefully we can make good use of this in our unit tests

No comments: