CNLearn FastAPI - Adding Coverage

A billion years later, but today we’ll add coverage to our continuous integration pipeline. This is the last CI thing we’ll be doing for a while (well maybe except adding a test failure when coverage decreases under a certain amount). It will be a short post!

So why do I want coverage? Well, I want 100% test coverage because I’m special like that. Currently we are on 89% or so. You might ask:

  • Does 100% coverage mean there won’t be any bugs? Nope, it does not mean that.
  • Is 100% coverage something we should always strive for? I think if it’s a new project and you can do it, sure. Otherwise, focus on testing the most important parts (only you can decide what those are)
  • Are you just making up questions and answers now? Yes I am.

Anyway, we are going to use a nice library called pytest-cov.

pip install pytest-cov

Then, let’s add that to our pytest config section in pyproject.toml.

[tool.pytest.ini_options]
minversion = "7.0"
asyncio_mode = "auto"
addopts = "--cov=app --cov-report=lcov:cov.info"

The first option, --cov=app is saying please calculate coverage for this package (I’m ignoring the alembic stuff, no need). Then, the --cov-report=lcov:cov.info is saying please make it in LCOV format (for people interested, go here) and save it in the cov.info file.

We are also going to use Coveralls for our project. Since it’s open source, it’s free so I connected the repo to it. Then, in order to let it use the cov.info file created by pytest and pytest-cov above, we need to add the following to our GitHub action file:

- name: Coveralls GitHub Action
  uses: coverallsapp/github-action@master
  with:
    path-to-lcov: "./cov.info"
    github-token: ${{ secrets.GITHUB_TOKEN }}

Please note we are telling the action to use the cov.info file that was created above. For more information about this specific GitHub action, please go here.

And that’s it. Now Coverage will run. oooh, I also added two nice badges to the Readme file :) One for the workflow file and one for the coverage status. The commits for this post are here (the main one) but followed by this one. Why two? Well in the first one I was pointing the coverage badge to the wrong branch :P It’s set to main in the second one.

Next post is new models, I promise :)