Chandra Sivaraman
Programming/Software Engineering Notes

Unit Testing Part 109 Dec 2020

Unit Testing Image by testbytes from Pixabay

What is a unit test?

A Unit Test is test code that exercises a subset (unit) of functionality referred to as the system under test (SUT). e.g. one method of a class or a standalone function.

What to test?

There are two approaches according to Fowler. The mockist and the classicist approach. According to the former, mock objects are used for both the SUT and dependencies. This is more of a white box approach that requires knowledge of implementation details. The classicist approach is what Fowler favors, and is the focus of this article. This is a black box approach in which the real SUT object is instantiated and invoked and dependencies such as database, web service calls, environment-specific things like configuration, cookies etc. (which should ideally be injected into the SUT) are simulated. Just like a taster doesn’t add sugar and milk when tasting tea, the idea is to only test the SUT without adding any external flavoring from dependencies. This is important for the following reasons:

Why write unit tests?

Test Doubles - Mocks, Fakes, Stubs, etc.

Continue to part 2.