How do I enforce a minimum test coverage percentage in my Foundry Code Repositories?

2k views Asked by At

I not only want to write unit tests in my Foundry Code Repository, but I want to enforce a minimum amount of coverage for checks to pass.

It's a best practice to ensure that I'm covering at least a certain fraction of my code with unit tests, but tests don't seem to be a requirement in standard repositories.

How do I accomplish my two testing goals within my Foundry Code Repository?

1

There are 1 answers

0
vanhooser On

You can use PyTest Coverage to compute coverage and enforce a minimum percentage.

  1. Add the following to your repo's meta.yml :
...

test:
  requires:
    - pytest-cov
...

Note: this is outside the requirements section in a section of its own.

  1. Create a pytest.ini file at /transforms-python/src/pytest.ini with the following contents:
[pytest]
addopts = --cov=<<package name, e.g. myproject>> --cov-report term --cov-fail-under=100

Note: you can tune the enforcement percentage down from 100 if you'd like

Your tests will now fail, even if they include some unit tests, if that coverage threshold is below your configured value:

checks