Unit Testing the Hyperparameter Class

Learn how to access the classify method using mock objects and testing strategies.

The Hyperparameter class relies on a distance computation. We have two strategies for testing a complex class like this:

  • An integration test that uses the distance computations already tested
  • A unit test that isolates the Hyperparameter class from any of the distance computations to be sure the class works

As a general rule of thumb, every line of code needs to be exercised by at least one unit test. After that, integration tests can also be used to ensure that the interface definitions are honored by all of the modules, classes, and functions. The spirit of test everything is more important than make the number come out right; counting lines is one way to ensure that we’ve tested everything.

Evaluating the classify() method

We’ll look at testing the classify() method of the Hyperparameter class using Mock objects to isolate the Hyperparameter class from any of the distance computations. We’ll also mock the TrainingData object to further isolate an instance of this class.

Here’s the relevant code we’ll be testing:

Get hands-on with 1200+ tech skills courses.