Coverage-based regression test selection (RTS) plugin for pytest
Project description
Coverage-based regression test selection (RTS) plugin for pytest
Usage
Plugin is supposed to be used to execute tests related to changes done locally on developer's machine and in CI environment to test pull requests.
Initialization
To start using pytest-rts build of coverage DB is needed. For Trunk Based Development mapping database from master
branch should be used, for A successful Git branching model - develop
- Install pytest-cov with
pip install pytest-cov
- Create a
.coveragerc
file with the following contents inside to configurepytest-cov
:
[run]
relative_files = True
- Execute
pytest --cov=[path to your package] --cov-context=test --cov-config=.coveragerc
which will run the entire test suite and build a mapping database in.coverage
file - Rename the coverage file
.coverage
produced bypytest-cov
to your liking. Example:mv .coverage pytest-rts-coverage
Note that --cov-config=.coveragerc
is optional parameter here. For more info see official docs.
Local usage
Tests from changes in Git working directory
- Install
pytest-rts
withpip install pytest-rts
- Create a branch
git checkout -b feat/new-feature
- Make changes in your code
- Run the tool with
pytest --rts --rts-coverage-db=[path to database]
As a result only tests related to changes in working directory will be executed.
Tests from changes in Git working directory + committed changes
- Install
pytest-rts
withpip install pytest-rts
- Create a branch
git checkout -b feat/new-feature
- Make changes in your code and commit them
- Run the tool with
pytest --rts --rts-coverage-db=[path to database] --rts-from-commit=[database initialization commithash]
The current git working directory copy will be compared to the given commithash. Tests for changes in commits and in the working directory will be executed.
Usage in CI
- In the main branch (
master
ordevelop
) make sure you run entire test suite and- commit back coverage database
- or, if the database size is big, upload it to some storage
- In pull requests:
- make sure you have coverage database from the main branch located next to the code
- run
pytest --rts --rts-coverage-db=[path to database] --rts-from-commit=[database initialization commithash]
One of the ways to organize it in Makefile would be:
test: BRANCH_NAME = $(shell git branch | sed -n -e 's/^\* \(.*\)/\1/p')
test:
@[ "$(BRANCH_NAME)" = "master" ] && $(MAKE) test-master || $(MAKE) test-pr
test-master:
pytest \
--exitfirst \
--cov \
--cov-context=test \
--cov-config=.coveragerc
mv .coverage mapping.db
git add mapping.db
git commit -m "test: updated RTS mapping DB"
git push
test-pr: MASTER_COMMIT = $(shell git merge-base remotes/origin/master HEAD)
test-pr:
git checkout $(MASTER_COMMIT) mapping.db
pytest \
--exitfirst \
--rts \
--rts-coverage-db=mapping.db \
--rts-from-commit=$(MASTER_COMMIT) && [ $$? -eq 0 ] || [ $$? -eq 5 ]
Exit code tests/overwrite && [ $$? -eq 0 ] || [ $$? -eq 5 ]
is needed in cases when no tests are found for execution.
See Troubleshooting section for more information.
Troubleshooting
pytest --rts
returns non-zero code: command returns one of the pytest exit codes. For example if pytest-rts module found no tests to execute resulting code will be 5 "No tests were collected"
Development
See DEVELOPER.md for more info
Contributing
Contributing Guidelines
Read through our contributing guidelines to learn about our submission process, coding rules and more.
Code of Conduct
Help us keep the project open and inclusive. Please read and follow our Code of Conduct.
Acknowledgement
The package was developed by F-Secure Corporation and University of Helsinki in scope of IVVES project. This work was labelled by ITEA3 and funded by local authorities under grant agreement “ITEA-2019-18022-IVVES”
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pytest_rts-2.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3474f6ac43506f16ea25587c21d2094570448a44c009e0e204c28c5e39781a69 |
|
MD5 | 46061be18bb08037b694e05a47678264 |
|
BLAKE2b-256 | ca453a59a8b0405257ebd6dd2ea936379c160e0c13cc65dd8f78b259b6ab1b89 |