Coverage Testing

Introduction

It is generally impossible to prove that code is correct through testing. The tests themselves may be incorrect, or there may be subtle defects that slip past even the most carefully written tests. On the other hand, the assumption when programming should always be “guilty until proven innocent”. It is difficult to write perfectly correct code on the first attempt, so code that hasn’t been tested is almost certain to contain defects.

This means that measuring test coverage is an important step in developing correct code. In its simplest form, “test coverage” is a measurement of the percentage of lines in our program that are actually executed when we run our unit tests. If this percentage is less than 100%, then we know there are lines of code in our program that are never executed during testing, and are therefore likely to contain errors.

Fortunately, there are automated tools that can report coverage statistics and point out lines of code that are missed during testing.

Transit Application and Tests

Download the following files and open them in Thonny

Take a few minutes to read over the docstrings in transit.py to get a feel for the requirements for this class. Run test_transit.py to confirm that none of the provided tests are failing. Don’t edit either file yet.

Coverage Tool

  • If you are working on your own computer, install the coverage package through the Thonny package manager.
  • If you are working on one of the lab machines, there will already be an older version of coverageinstalled. Unfortunately, the Thonny package manager isn’t able to upgrade to the latest version, so you’ll need to do it manually. Open a terminal and type the following:

    pip install --user --upgrade coverage
  • Once the package is installed, you should be able run your tests using the coverage tool in the Thonny shell:

    >>> !coverage run test_transit.py
    ..
    ----------------------------------------------------------------------
    Ran 2 tests in 0.000s
    OK

    You’ll need to re-run this tool each time you make a change to your tests.

  • The next step is to generate an HTML coverage report that can be viewed in a web browser:

    >>> !coverage html
    Wrote HTML report to htmlcov/index.html
  • Once this page has been created, you should be able to double-click it in your file manager, or if you are on the lab machines you could type the following in the Thonny shell:

    >>> !firefox htmlcov/index.html

    Clicking on transit.py in the resulting web page will show which lines are executed and which lines are missed by the current tests.

Finishing the Code and Improving Test Coverage

Your goal now is twofold:

  1. Complete transit.py so that all methods conform to the requirements described in the docstring.

  2. Improve the provided tests so that they provide 100% coverage transit.py.

Once both files are completed, submit your work to Gradescope.

Keep in mind that 100% test coverage DOESN’T MEAN THAT YOUR CODE IS CORRECT. In this case, if your tests provide 100% coverage, but your Bus class doesn’t pass our submission tests, that’s a good indication that there is room for improvement in your tests.

Back to Top