Skip to main content

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

  1. Install pytest-cov with pip install pytest-cov
  2. Create a .coveragerc file with the following contents inside to configure pytest-cov:
[run] 
relative_files = True
  1. 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
  2. Rename the coverage file .coverage produced by pytest-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

  1. Install pytest-rts with pip install pytest-rts
  2. Create a branch git checkout -b feat/new-feature
  3. Make changes in your code
  4. 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

  1. Install pytest-rts with pip install pytest-rts
  2. Create a branch git checkout -b feat/new-feature
  3. Make changes in your code and commit them
  4. 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 or develop) 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:

BRANCH_NAME = $(shell git branch | sed -n -e 's/^\* \(.*\)/\1/p')
ifeq ($(BRANCH_NAME), master)
test: test-master
else
test: test-pr
endif

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.

Usage with coverage threshold

You might desire to use pytest-cov with the --cov-fail-under=MIN flag. When using pytest-rts this is somewhat possible but the reported coverage percentage will not represent actual coverage. If you wish to combine the usage of --rts and the coverage threshold, do the following:

  1. Initialize the coverage database as usual
  2. Leave the .coverage file untouched or rename it to start with .coverage.. For example .coverage.rts.db
  3. Do your changes and add --cov=[path to code] and --cov-append to the flags. For example pytest --rts --rts-coverage-db=.coverage --cov=. --cov-append
  4. pytest-cov will automatically combine the coverage file for the previous full run and the current RTS run. Bear in mind that the coverage file will now have added data. Create a backup of your full test run coverage file if you wish to keep it intact.

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”

Metadata

Release files for pytest-rts 2.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-rts 2.5.0
File Size Uploaded
pytest-rts-2.5.0.tar.gz 1.4 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-rts 2.5.0
File Interpreter ABI Platform
pytest_rts-2.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.4 MB

Release files / pytest-rts-2.5.0.tar.gz

Download URL pytest-rts-2.5.0.tar.gz
Size 1.4 MB
Tags Source
SHA-256 checksum
How to use checksums
df7b743d3c6c01bffa105b39e2ca4950b02d78cf1b6a3a8cd901bc76c5451df9
BLAKE2b-256 checksum
How to use checksums
08ae148d7aa7b987c7e67345b1f0170c0db73a05271ff89f5ecba5fb66cbea19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.6.13

Release files / pytest_rts-2.5.0-py3-none-any.whl

Download URL pytest_rts-2.5.0-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
22be0f9adce5005e5e339eed1e91ed1c79c11eca0da9fa6bbb5bca2ca63273ee
BLAKE2b-256 checksum
How to use checksums
8945b2a93c728909673c941514038c7157c63169cde1ab0a5200fdf60bde5d2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.6.13

Release history Release notifications | RSS feed

This release

2.5.0 This release

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.10

2 release files

1.1.9

2 release files

1.1.7

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page