6 Styles of unit testing

 

This chapter covers:

  • Comparing styles of unit testing
  • The relationship between functional and hexagonal architectures
  • Transitioning to output-based testing

Chapter 4 introduced the four attributes of a good unit test: protection against regressions, resistance to refactoring, fast feedback, and maintainability. These attributes form a frame of reference that you can use to analyze specific tests and unit testing approaches. We analyzed one such approach in chapter 5: the use of mocks.

In this chapter, I apply the same frame of reference to the topic of unit testing styles. There are three such styles: output-based, state-based, and communication-based testing. Among the three, the output-based style produces tests of the highest quality, state-based testing is the second-best choice, and communication-based testing should be used only occasionally.

Unfortunately, you can’t use the output-based testing style everywhere. It’s only applicable to code written in a purely functional way. But don’t worry; there are techniques that can help you transform more of your tests into the output-based style. For that, you’ll need to use functional programming principles to restructure the underlying code toward a functional architecture.

6.1  The three styles of unit testing

6.1.1  Defining the output-based style

6.1.2  Defining the state-based style

6.1.3  Defining the communication-based style

6.2  Comparing the three styles of unit testing

6.2.1  Comparing the styles using the metrics of protection against regressions and feedback speed

6.2.2  Comparing the styles using the metric of resistance to refactoring

6.2.3  Comparing the styles using the metric of maintainability

6.2.4  Comparing the styles: the results

6.3  Understanding functional architecture

6.3.1  What is functional programming?

6.3.2  What is functional architecture?

6.3.3  Comparing functional and hexagonal architectures

6.4  Transitioning to functional architecture and output-based testing

6.4.1  Introducing an audit system

6.4.2  Using mocks to decouple tests from the filesystem

6.4.3  Refactoring toward functional architecture

sitemap