Code Coverage and Quality

Part 2, Chapter 2

In this chapter, we'll add code coverage via to the project.

Point the Docker client back to the localhost:

$ eval $(docker-machine env -u)

Update the containers:

$ docker-compose up -d

Ensure the app is working in the browser, and then run the tests:

$ docker-compose exec users python test

Code Coverage

Code coverage is the process of finding areas of your code not covered by tests. is a popular tool for measuring code coverage in Python-based applications.

Add to the requirements.txt file in the "users" directory:


Next, we need to configure the coverage reports in Start by adding the configuration right after the imports:

COV = coverage.coverage(

Then add a new CLI command:

def cov():
    """Runs the unit tests with coverage."""
    tests = unittest.TestLoader().discover('project/tests')
    result = unittest.TextTestRunner(verbosity=2).run(tests)
    if result.wasSuccessful():
        print('Coverage Summary:')
        return 0

Don't forget the import!

import coverage

Update the containers:

$ docker-compose up -d --build

Run the tests with coverage:

$ docker-compose exec users python cov

You should see something like:

Coverage Summary:
Name                    Stmts   Miss Branch BrPart  Cover
project/        14      6      0      0    57%
project/api/      14     11      0      0    21%
project/api/       56      0     10      0   100%
TOTAL                      84     17     10      0    82%

The HTML version can be viewed within the newly created "htmlcov" directory. With it, you can quickly see which parts of the code are, and are not, covered by a test.

$ open services/users/htmlcov/index.html

Add this directory to the .gitignore and .dockerignore files.

Just keep in mind that while code coverage is a good metric to look at, it does not measure the overall effectiveness of the test suite. In other words, having 100% coverage means that every line of code is being tested; it does not mean that the tests handle every scenario.

In other words, just because you have 100% test coverage doesn’t mean you're testing the right things.

Code Quality

Linting is the process of checking your code for stylistic or programming errors. Although there are a number of commonly used linters for Python, we'll use Flake8 since it combines two other popular linters—pep8 and pyflakes.

Add flake8 to the requirements.txt file in the "users" directory:


Update the containers:

$ docker-compose up -d --build

Run flake8:

$ docker-compose exec users flake8 project

Were any errors found?

project/api/ E122 continuation line missing indentation or outdented
project/api/ E122 continuation line missing indentation or outdented
project/api/ E122 continuation line missing indentation or outdented
project/api/ E501 line too long (80 > 79 characters)
project/tests/ E303 too many blank lines (2)

Correct any issues before moving on. Commit your code, and push it to GitHub.

Black is another recommended tool, which is used for formatting your code so that "code looks the same regardless of the project you're reading". This helps to speed up code reviews. "Formatting becomes transparent after a while and you can focus on the content instead." Implement this on your own.

Mark as Completed