Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add coverage reporting to tests #917

Merged
merged 2 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[run]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you may need parallel = True and branch = True as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

branch = True, yes I should add that. parallel = True is being set by pytest-cov.

source = ansible_runner
data_file = test/coverage/data/coverage

[report]
skip_covered = True
skip_empty = True

[html]
directory = test/coverage/reports/html

[xml]
output = test/coverage/reports/coverage.xml

[json]
output = test/coverage/reports/coverage.json
56 changes: 31 additions & 25 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
# Mac OS X
*.DS_Store
# Test artifacts
*.py,cover
.pytest_cache/
docs/_build/
docs/build/
pytestdebug.log
test/coverage/

# Byte-complied files
*.py[cod]
__pycache__/

# Editors
*.sw[poj]
*~
/.idea
# Distribution / packaging
*.egg
*.egg-info/
.eggs/
AUTHORS
Changelog
build/
deb-build/
dist/
eggs/
rpm-build/

# Environments
.env
.python-version
.tox/
.venv
env/
venv/

# Demo files
/demo/artifacts
/demo/daemon.log
/docs/_build
/.tox
/dist
/build
/rpm-build
/deb-build
/*.egg-info
*.py[c,o]
.pytest_cache
pytestdebug.log
.coverage
*,cover
.venv
/venv
.env
/.eggs/
docs/build
AUTHORS
ChangeLog
4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ addopts =
--durations 10
--durations-min 1
--strict-markers
--cov
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samdoran I also recommend adding an explicit -p pytest_cov to make sure that this plugin is pre-loaded earlier (this is sometimes important) rather than later (on the plugin discovery stage).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another comment is that you should measure the coverage for both the project and the tests. For this, you'd need to use a few subsequent args: --cov=ansible_runner --cov=test/. It may be really helpful to find out if some of the tests never run in CI, for example.

Here's what the coveragepy author posted last year: https://nedbatchelder.com/blog/202008/you_should_include_your_tests_in_coverage.html.

--cov-report html
--cov-report term
--cov-report xml
1 change: 1 addition & 0 deletions test/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pytest
pytest-cov
pytest-mock
pytest-timeout
pytest-xdist
Expand Down
9 changes: 9 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,12 @@ description = Build documentation
deps = -r{toxinidir}/docs/requirements.txt
commands =
sphinx-build -T -E -W --keep-going {tty:--color} -j auto -d docs/build/doctrees -b html docs docs/build/html

[testenv:clean]
description = Erase docs and coverage artifacts
deps =
skip_install = True
allow_external = /bin/sh
commands =
/bin/sh -c "rm -rf {toxinidir}/test/coverage/*"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samdoran I have a few comments here:

  1. You could've used a combination of git clean commands which is easier to maintain, here's an example: https://github.com/ansible/pylibssh/blob/924771a/tox.ini#L369-L380.
  2. Instead of calling sh/rm, you could do {envpython} -c 'import shutil; shutil.rmdir("{toxinidir}/test/coverage/")' and avoid having to use allow_externals.

/bin/sh -c "rm -rf {toxinidir}/docs/{_,}build"