Facebook Pixel
Searching...
English
EnglishEnglish
EspañolSpanish
简体中文Chinese
FrançaisFrench
DeutschGerman
日本語Japanese
PortuguêsPortuguese
ItalianoItalian
한국어Korean
РусскийRussian
NederlandsDutch
العربيةArabic
PolskiPolish
हिन्दीHindi
Tiếng ViệtVietnamese
SvenskaSwedish
ΕλληνικάGreek
TürkçeTurkish
ไทยThai
ČeštinaCzech
RomânăRomanian
MagyarHungarian
УкраїнськаUkrainian
Bahasa IndonesiaIndonesian
DanskDanish
SuomiFinnish
БългарскиBulgarian
עבריתHebrew
NorskNorwegian
HrvatskiCroatian
CatalàCatalan
SlovenčinaSlovak
LietuviųLithuanian
SlovenščinaSlovenian
СрпскиSerbian
EestiEstonian
LatviešuLatvian
فارسیPersian
മലയാളംMalayalam
தமிழ்Tamil
اردوUrdu
The Art of Unit Testing

The Art of Unit Testing

With examples in C#
by Roy Osherove 2009 266 pages
4.09
1k+ ratings
Listen

Key Takeaways

1. Unit testing is essential for code quality and developer confidence

A unit test is an automated piece of code that invokes the unit of work being tested, and then checks some assumptions about a single end result of that unit.

Definition of unit testing. Unit testing involves writing automated code to verify the behavior of small, isolated pieces of functionality within a software system. It helps developers catch bugs early, refactor with confidence, and document expected behavior. By focusing on testing individual units of work, developers can ensure that each component functions correctly before integration.

Benefits of unit testing:

  • Improves code quality by catching bugs early
  • Enables safer refactoring and code changes
  • Serves as living documentation for code behavior
  • Increases developer confidence in the codebase

2. NUnit provides a robust framework for writing and running unit tests

NUnit uses an attribute scheme to recognize and load tests.

Key features of NUnit. NUnit is a popular testing framework for .NET that simplifies the process of writing and running unit tests. It provides attributes to mark test methods and classes, assertion methods to verify expected outcomes, and a test runner to execute tests and report results.

Core NUnit concepts:

  • [Test] attribute for marking test methods
  • [TestFixture] attribute for test classes
  • Assert class for verifying test outcomes
  • Test runners for executing tests (GUI, console, or IDE integration)

3. Stubs and mocks are crucial for isolating dependencies in tests

A stub is a controllable replacement for an existing dependency (or collaborator) in the system.

Understanding test doubles. Stubs and mocks are types of test doubles used to isolate the code under test from its dependencies. Stubs provide predetermined responses to method calls, while mocks allow verification of interactions between objects.

Key differences:

  • Stubs: Return fixed values, don't fail tests
  • Mocks: Verify method calls, can cause test failures
  • Use stubs for state testing, mocks for interaction testing
  • Limit to one mock per test to maintain focus

4. Isolation frameworks simplify the creation of test doubles

An isolation framework is a set of programmable APIs that makes creating fake objects much simpler, faster, and shorter than hand-coding them.

Benefits of isolation frameworks. These tools automate the creation of stubs and mocks, reducing the need for manual implementation. They offer powerful APIs for configuring behavior and verifying interactions, making tests more concise and easier to maintain.

Popular isolation frameworks:

  • Moq: Open-source, lambda-based API
  • NSubstitute: Simple, readable syntax
  • Typemock Isolator: Commercial, supports legacy code
  • FakeItEasy: Intuitive API, good for beginners

5. Test organization and hierarchy are key to maintainable test suites

Mapping tests to projects, classes, and methods is crucial for maintainability and readability.

Structuring test projects. Organize tests to mirror the structure of the production code, making it easy to locate and manage related tests. Use clear naming conventions and create a hierarchy that reflects the system under test.

Test organization best practices:

  • One test project per production project
  • One test class per production class
  • Test method names describe the scenario and expected outcome
  • Group related tests using test categories or nested classes

6. Good unit tests are trustworthy, maintainable, and readable

Without readability the tests you write are almost meaningless.

Characteristics of quality tests. Effective unit tests should be reliable, easy to update, and comprehensible to other developers. Focus on writing tests that clearly communicate their purpose and expectations.

Guidelines for better tests:

  • Use descriptive test names (method_scenario_expectedOutcome)
  • Follow the Arrange-Act-Assert pattern
  • Avoid logic and multiple assertions in tests
  • Keep tests isolated and independent
  • Use meaningful variable names and clear assertions

7. Implementing unit testing in an organization requires careful strategy

There are two main ways an organization or team can start changing a process: bottom up or top down (and sometimes both).

Approaches to adoption. Introducing unit testing to an organization can be challenging. It requires a combination of technical skills, cultural change, and management support. Choose an approach that fits your organization's culture and constraints.

Implementation strategies:

  • Start with a pilot project or team
  • Provide training and resources
  • Set clear goals and metrics
  • Address common objections and concerns
  • Celebrate successes and learn from failures

8. Legacy code can be made testable with the right approach and tools

Designing for testability becomes a way of thinking.

Tackling untested code. Legacy code often lacks tests and can be difficult to modify. By using specific techniques and tools, developers can gradually introduce tests and improve the code's testability.

Strategies for legacy code:

  • Use characterization tests to document existing behavior
  • Apply the "Sprout Method" technique to add new, tested functionality
  • Leverage unconstrained mocking frameworks for hard-to-isolate dependencies
  • Refactor gradually to improve testability
  • Prioritize high-risk or frequently changing areas

9. Designing for testability enhances overall code quality

Testable designs usually only matter in static languages, such as C# or VB.NET, where testability depends on proactive design choices that allow things to be replaced.

Benefits of testable design. Designing code with testability in mind often leads to better overall architecture. It promotes loose coupling, clear interfaces, and separation of concerns, which are valuable beyond just testing.

Testability design principles:

  • Dependency injection for easier mocking
  • Interface-based design for flexibility
  • Single Responsibility Principle for focused classes
  • Avoid static methods and global state
  • Use factory methods or abstract factories for object creation

Last updated:

Review Summary

4.09 out of 5
Average of 1k+ ratings from Goodreads and Amazon.

The Art of Unit Testing receives generally positive reviews, with readers praising its comprehensive coverage of unit testing concepts and practices. Many find it helpful for beginners, while some experienced developers note its usefulness in refining their skills. The book's practical examples and clear explanations are appreciated. However, some criticize its heavy focus on C# and outdated information. Readers value the insights on test organization, mocking, and integrating testing into development processes. A few reviewers find the content repetitive or contradictory to other sources.

Your rating:

About the Author

Roy Osherove is a software development expert and author known for his work on unit testing and software craftsmanship. He has extensive experience in the tech industry, having worked as a consultant, trainer, and team leader. Roy Osherove is recognized for his contributions to the field of automated testing and has authored multiple books on the subject. He is a frequent speaker at conferences and has developed training courses on unit testing and test-driven development. Osherove's approach to teaching complex concepts through practical examples and clear explanations has made him a respected figure in the software development community.

Download PDF

To save this The Art of Unit Testing summary for later, download the free PDF. You can print it out, or read offline at your convenience.
Download PDF
File size: 0.34 MB     Pages: 9

Download EPUB

To read this The Art of Unit Testing summary on your e-reader device or app, download the free EPUB. The .epub digital book format is ideal for reading ebooks on phones, tablets, and e-readers.
Download EPUB
File size: 3.08 MB     Pages: 7
0:00
-0:00
1x
Dan
Andrew
Michelle
Lauren
Select Speed
1.0×
+
200 words per minute
Create a free account to unlock:
Bookmarks – save your favorite books
History – revisit books later
Ratings – rate books & see your ratings
Unlock unlimited listening
Your first week's on us!
Today: Get Instant Access
Listen to full summaries of 73,530 books. That's 12,000+ hours of audio!
Day 4: Trial Reminder
We'll send you a notification that your trial is ending soon.
Day 7: Your subscription begins
You'll be charged on Jan 3,
cancel anytime before.
Compare Features Free Pro
Read full text summaries
Summaries are free to read for everyone
Listen to summaries
12,000+ hours of audio
Unlimited Bookmarks
Free users are limited to 10
Unlimited History
Free users are limited to 10
What our users say
30,000+ readers
“...I can 10x the number of books I can read...”
“...exceptionally accurate, engaging, and beautifully presented...”
“...better than any amazon review when I'm making a book-buying decision...”
Save 62%
Yearly
$119.88 $44.99/yr
$3.75/mo
Monthly
$9.99/mo
Try Free & Unlock
7 days free, then $44.99/year. Cancel anytime.
Settings
Appearance
Black Friday Sale 🎉
$20 off Lifetime Access
$79.99 $59.99
Upgrade Now →