Introduction

Welcome to the very last lesson of the "Increasing Code Test Coverage" course! In our journey so far, we've explored the importance of code coverage, the role of characterization tests, and how abstract classes and mocks can enhance testability. Now, we'll bring these concepts together to achieve comprehensive code coverage by integrating test writing and mocking techniques. This lesson will help you solidify your understanding and prepare you for practical application.

Whole Problem: The UserProcessor Class

Let's take a closer look at an implementation of a user processor. We want to add tests in order to describe the characteristics of the system. We also don't want our tests to depend on a database. Let's take a closer look at the implementation of the UserProcessor class:

The UserProcessor class uses a UserDatabase class as a dependency. The UserDatabase class is designed to be overridden, allowing us to mock it in tests.

Practice Approach

First, we will focus on adding an abstract class for the database layer to enable us to mock this particular dependency. After that, we will focus on writing a collection of tests that will describe the system and help us increase code test coverage for the UserProcessor class.

Defining Interfaces and Mocks in C++

In C++, interfaces are typically defined using abstract classes. We can use a library like Google Mock to create mock objects for testing. Here's a brief example of how you might define a mock for UserDatabase:

Best Practices for Test Writing

When writing tests with mocks, consider the following best practices:

  • Focus on Behavior: Ensure tests verify the behavior of the code, not the implementation details.
  • Use Descriptive Names: Name tests clearly to indicate the scenario being tested.
  • Verify Interactions: Use mock verifications to ensure the correct methods are called.
  • Handle Exceptions: Test how the code handles exceptions to ensure robustness.

By following these practices, you can create reliable and maintainable tests that provide confidence in your code.

Practice Preparation

As you move on to the practice exercises, you'll have the opportunity to apply all the things you have learned so far in this course. Good luck, and enjoy the journey of mastering code test coverage!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal