Chapter 13. Testing

Conceptually, testing in Clojure is largely the same as it is in Java, Python, or Ruby. Regardless of the language you use, the objective is always to:

  1. Construct a suitable environment.

  2. Run some code.

  3. Verify that the code behaved or returned as expected.

Of course, the details of each piece of this task can vary greatly between languages and testing frameworks. In this chapter, we’ll survey ways to test in Clojure, focusing on clojure.test, the test framework that is included in the language’s standard library.

Immutable Values and Pure Functions

In an object-oriented language like Java or Python or Ruby, testing can be a complicated matter. Objects tend to have numerous, subtle interactions with each other. Mutating one object may end up mutating any number of other objects, or the behavior of one object may implicitly rely upon the state of others. These interactions are often combinatorial in nature, making it difficult to reliably account for all the environmental characteristics that can change our programs’ behavior, and therefore more difficult to test.

As we detailed in Chapter 2, Clojure encourages the use of immutable values and pure functions. Code written in line with such sensibilities is greatly simplified from a testing perspective: if your functions’ results are determined only by their arguments, then simple unit tests are all that are required to ensure suitable test coverage. Of course, integration and functional testing will still be necessary for ...

Get Clojure Programming now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.