I’ve just read the book “The Art of Unit Testing” by Roy Osherove. To tell the truth my previous knowledge on writing Unit tests was rather superficial with basic usage of nUnit and main experience in writing old-school C++ Unit tests without using any framework. My overall impression on the book is very good. The author has clear and systematic writing style, brings complete lists of pros & cons for approaches and options described.
The main ideas I got from the Book are the following:
- Good Unit test should meet the following properties:
If Unit tests do not satisfy these criteria (described in detail in the Book) then there will be very little use of them (or even harm and time waste) especially if code under the tests is changed afterwards.
- Developers often mix Unit tests with integration tests. Roy argues that real Unit tests should not have any dependencies on external subsystems, services, modules, etc. and should be executed purely in RAM. I was aware that Unit test shouldn’t interact with Database or network but it was surprising for me that UT shouldn’t also use filesystem or even system time. Author states that every test that has such external dependencies is actually an Integration test. It’s ok to have integration tests but they should be separated from true Unit tests and should be treated in a different way because they don’t have main properties of UT – trustworthiness, maintainability, high execution speed, etc.
- The Author praises test-driven development (TDD) throughout the book. I haven’t ever tried this software development technique, but Roy is so convincing that I’ll definitely study Kent Beck’s book “Test-Driven Development: by Example” and will try it by myself.
The Book has plenty examples based on nUnit and nSubstitute frameworks. Roy often starts with example of Unit test as it is usually developed, then describes what wrong is with this UT and finally refactors test code to fix the problems in it. The book also has an appendix that enumerates available isolation and test frameworks with short description for each of them.
“The Art of Unit Testing” refers to several other books I’ve added to my to-read list:
- “Test Driven Development: By Example” by Kent Beck
- “Working Effectively with Legacy Code” by Michael Feathers
- “Clean Code: A Handbook of Agile Software Craftsmanship” by Robert C. Martin
- “xUnit Test Patterns: Refactoring Test Code” by Gerard Meszaros