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 Mar 14, 2024
1 parent eeafb6a commit f153963
Show file tree
Hide file tree
Showing 7 changed files with 75 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
47 changes: 47 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,50 @@ jobs:
- name: Run tests
run: |
make ci
- name: "Upload artifact"
uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
with:
name: coverage-data-${{ matrix.python-version }}
path: ".coverage.*"
if-no-files-found: error

coverage:
name: "Combine & check coverage"
needs: build
runs-on: "ubuntu-latest"

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 -Im pip install --upgrade coverage

- name: "Download coverage artifacts"
uses: actions/download-artifact@v4
with:
pattern: coverage-data-*
merge-multiple: true

- name: Combine coverage & fail if it's <100%.
run: |
python -Im coverage combine
python -Im coverage html --skip-covered --skip-empty
# Report and write to summary.
python -Im coverage report --format=markdown >> $GITHUB_STEP_SUMMARY
# Report again and fail if under 100%.
python -Im coverage report --show-missing --fail-under=85
- name: "Upload HTML report if check failed."
uses: actions/upload-artifact@v4
with:
name: html-report
path: htmlcov
if: ${{ failure() }}
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 {posargs:tests}
coverage run --parallel-mode -m pytest
coverage report

[testenv:default]

Expand Down

0 comments on commit f153963

Please sign in to comment.