Test-Driven Development and Refactoring At Agile Roots 2009

I was a guest instructor at Zhon Johansen’s Test-Driven Development and Refactoring course at the recent Agile Roots Conference in Salt Lake City. Zhon and I did a dry-run rehearsal of this course for the Utah .NET User Group meeting in June. The streaming video for this tutorial is now available.

Read the rest of this entry »

C++ Unit Tests With Boost.Test, Part 5

In Part 4 of this series, we completed some UI functionality using a test-driven development style. In this final part of the series, I will cover the facilities in Boost.Test for sharing common setup and teardown actions between tests and organizing tests into suites.

Read the rest of this entry »

C++ Unit Tests With Boost.Test, Part 4

In Part 3 of this series, we completed Bob Martin’s Prime Factors Kata using test-driven development and unit tests written in C++ with Boost.Test. In this part, I will discuss test-driven UI development.

Read the rest of this entry »

C++ Unit Tests With Boost.Test, Part 3

In Part 2 of this series, I discussed the assertion facilities of Boost.Test and how to get the first passing test on Bob Martin’s Prime Factors Kata. In this part, I will continue with test-driven development to complete the functionality of the PrimeFactors::Generate method.

Read the rest of this entry »

C++ Unit Tests With Boost.Test, Part 2

In Part 1 of this series, I discussed how to setup a basic C++ unit test project that executed the unit tests when you built your code. In this part, I will discuss the assertion framework provided by Boost.Test in the context of a simple programming exercise that demonstrates test-driven development.

Read the rest of this entry »

C++ Unit Tests With Boost.Test, Part 1

In this post, I will describe how to start making unit tests in C++ with Boost.Test.

Read the rest of this entry »

How Do I Unit Test Something That Writes To A File?

This is a question that comes up periodically. Remember that a good unit test is one that is fast (< 100ms/test). A test that accesses files, databases or the network is not going to be this fast. But, we still have code that writes data to files or reads data from files and we want to unit test that code. So how can we do that?

Read the rest of this entry »

Writing Unit Tests Against Direct3D

Writing a unit test involves holding the “system under test” in a software vise and controlling all the other interactions of the system under test with the rest of the system. In an object-oriented programming language, the easiest way to control the interactions of your system under test with the rest of the universe is to interact with the universe through polymorphic interfaces. Because your system under test is collaborating with the universe through interfaces, it is not coupled to other concrete classes in your application. To unit test the system and its interactions, you provide “fake” or “mock” objects implementing the interfaces of the system’s collaborators. The system under test has no idea its collaborating with fake objects because it is coupled to an interface, not a concrete class.

Read the rest of this entry »

Faking Out The Win32 API With Templates

When writing a unit test, you want to hold the code under test in a vice. You want the code under test to collaborate with objects whose behavior you control so that you can inject controlled conditions into the code under test. This is fairly straightforward when the code under test collaborates with a class that implements an interface. You can simply write a test harness class with the desired behavior. The test harness class is usually called a “fake” object or a “mock” object. It is not the real collaborator, but an object whose purpose is to inject the desired conditions into the code under test to validate its behavior under different circumstances.

However, Win32 code has another sort of collaborator that is more difficult to fake — the global functions in the Win32 API. What happens when your code calls LoadLibrary and the file can’t be found? Furthermore, with a true unit test you don’t even want to do file I/O because that will make the test run slow. You want unit tests to run as fast as possible because you will be running them often as you make changes to the production code.

Read the rest of this entry »

“Working Effectively with Legacy Code” by Michael C. Feathers

I’ve just finished reading “Working Effectively with Legacy Code” by Michael C. Feathers.  This is a book that explains a variety of strategies and techniques for transforming a code base without unit tests to a code base with unit tests.  Unit tests are small, quick tests of software components that validate their behavior. 

Read the rest of this entry »