Skip to content

Commit

Permalink
Measure and report test coverage
Browse files Browse the repository at this point in the history
This runs pytest within coverage, enables pytest-xdist to improve test
run times, and reports the coverage on GitHub Actions for us.

This also ensures that the parallel mode coverage files are ignored and
adds things to pytest configuration to make tox command simpler
  • Loading branch information
sigmavirus24 committed Feb 24, 2024
1 parent 7a13c04 commit 0af0ee6
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 13 deletions.
14 changes: 13 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
[run]
omit = requests/packages/*
include = */requests/*.py
omit = */requests/packages/*
parallel = True

[report]
omit =
*/requests/packages/*

[paths]
source =
src/requests/
*/requests/
*\requests
40 changes: 40 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,43 @@ jobs:
- name: Run tests
run: |
make ci
- name: "Upload artifact"
uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
with:
name: coverage-data
path: ".coverage.*"
if-no-files-found: error

coverage:
if: always()
runs-on: "ubuntu-latest"
needs: build
steps:
- name: "Checkout repository"
uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0

- name: "Setup Python"
uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # v5.0.0
with:
python-version: "3.x"

- name: "Install coverage"
run: "python -m pip install -r dev-requirements.txt"

- name: "Download artifact"
uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
with:
name: coverage-data

- name: "Combine & check coverage"
run: |
python -m coverage combine
python -m coverage html --skip-covered --skip-empty
python -m coverage report --ignore-errors --show-missing --fail-under=85
- if: ${{ failure() }}
name: "Upload report if check failed"
uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
with:
name: coverage-report
path: htmlcov
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.coverage
.coverage*
MANIFEST
coverage.xml
nosetests.xml
Expand Down
13 changes: 7 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ test:
# This runs all of the tests on all supported Python versions.
tox -p
ci:
python -m pytest tests --junitxml=report.xml
python -m coverage run -m pytest --junitxml=report.xml

test-readme:
python setup.py check --restructuredtext --strict && ([ $$? -eq 0 ] && echo "README.rst and HISTORY.rst ok") || echo "Invalid markup in README.rst or HISTORY.rst!"
python -m pip install twine build
python -m build
python -m twine check dist/*

flake8:
python -m flake8 src/requests

coverage:
python -m pytest --cov-config .coveragerc --verbose --cov-report term --cov-report xml --cov=src/requests tests
python -m coverage run -m pytest

publish:
python -m pip install 'twine>=1.5.0'
python setup.py sdist bdist_wheel
python -m pip install 'twine>=1.5.0' build
python -m build
twine upload dist/*
rm -fr build dist .egg requests.egg-info

docs:
cd docs && make html
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ src_paths = ["src/requests", "test"]
honor_noqa = true

[tool.pytest.ini_options]
addopts = "--doctest-modules"
addopts = "--doctest-modules -n auto"
doctest_optionflags = "NORMALIZE_WHITESPACE ELLIPSIS"
minversion = "6.2"
testpaths = ["tests"]
7 changes: 4 additions & 3 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
-e .[socks]
pytest>=2.8.0,<=6.2.5
pytest-cov
pytest-httpbin==2.0.0
pytest>=2.8.0,!=6.2.5
coverage
pytest-xdist
httpbin~=0.10.0
pytest-httpbin==2.0.0
trustme
wheel
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ extras =
security
socks
commands =
pytest tests
coverage run --parallel-mode -m pytest
coverage report

[testenv:default]

Expand Down

0 comments on commit 0af0ee6

Please sign in to comment.