While working on a legacy application for a client, I wanted to get some code coverage for my tests. In Python, this usually means running Coverage.py and pointing it to your unit tests. This being a legacy application, there were no unit tests. There were Postman tests however.

This is a simple technique that is useful when working with legacy applications. The idea is simple: we run our application using Coverage.py. The application is a Django application and so it will just spin up a local web server and host the website:

coverage run --omit="envs/*" ./manage.py runserver --noreload 0.0.0.0:8000

Now we can run our Postman tests, which makes calls to our webserver:

Now, we can press CTRL+C to stop our Django application and generate our report:

coverage html

And we’re done! We now have a useful code coverage report:

This is incredibly useful in this legacy application because we have quite some refactoring before us. But before we do that, it’s great to have a good safety net with our tests. The code coverage report can help us identify where we still might need to add tests.

Even though unit tests would be better, we have to do with what we have when working with legacy applications. Once we have a coverage percentage we’re OK with, we can start refactoring and check that all our tests still pass.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.