Skip to main content

pytest-git-selector

Introduction

pytest-git-selector is a simple project to automatically select tests that have been affected by code changes. This is mainly useful for reducing the number of tests in frequently run CI/CD jobs on feature branches (e.g. running tests based on differences between the merge base and the feature branch).

Idea

The idea is fairly simple:

  1. Changes are analyzed using a git diff command. The arguments to git diff are supplied by the user
  2. If a test module contains one of the changed files in its import tree, then select that test module

The import tree is created using importlab which statically analyzes import statements to determine dependencies.

See the git-diff documentation for arguments that can be supplied to git diff.

pytest plugin

The plugin will automatically deselect tests whose descendants in the dependency tree have not changed. The changed are determined via a git diff command that is called with user supplied argument. User supplied arguments to git diff must be appear at the end of the pytest arguments separated from the rest of the pytest arguments using the -- delimiter e.g.

pytest --collect-only -- --diff-filter=M HEAD~1...

This plugin adds two additional flags to pytest:

  1. --src-path - specifies the directory containing the source code for the project. src and the current working directory are automatically added so this argument should not be required in most cases
  2. --extra-deps-file - specifies a path to a file containing dependencies between files not captured by Python import statements e.g. test input files. Edges should be in the form '(a.py,b.json)' where a.py depends on b.json. Edges separated by a space or newline. NOTE there is NO space after the comma. If edges are specified using relative paths, they interpreted as being relative to the directory containing the project root directory containing the .git folder.

Examples

The following examples assume the project contains source code in <project_root>/src/ and tests in <project_root>/test/. pytest is run in <project_root> in all the following examples.

Selecting tests affected by changes between a feature branch and its merge base

pytest -- main...

Selecting tests affected by changes in the last commit

pytest -- HEAD~1...

Selecting tests affected by changes between two commits with an extra dependency file extra_deps.txt specified and source code in custom_src_dir/

pytest --src-path custom_src_dir/ --extra-deps-file extra_deps.txt -- 1b23b4b1...12da93k8

Command line tool

The git-select-tests command line tool can be used to print out selected test modules. This is mainly useful for projects that do not use pytest as a test runner e.g. nosetest. The arguments to the git diff command are separated by the -- delimiter. See git-select-tests --help for usage.

Examples

The following examples assume the project contains source code in <project_root>/src/ and tests in <project_root>/test/.

Selecting tests affected by changes between a feature branch and its merge base

git-select-tests --src-path src/ --test-path test/ -- main...

Selecting tests affected by changes in the last commit

git-select-tests --src-path src/ --test-path test/ -- HEAD~1...

Comparison with pytest-diff-selector

This idea has been implemented before in this pytest-diff-selector project. The main differences are:

  1. pytest-git-selector leverages the importlab library as opposed to using a proprietary static analyzer
  2. pytest-git-selector implements a pytest plugin

Currently pytest-diff-selector does not appear to be working and has not been updated recently which is the reason for this project's existence.

Release files for pytest-git-selector 0.0.1

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-git-selector 0.0.1
File Size Uploaded
pytest-git-selector-0.0.1.tar.gz 10.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-git-selector 0.0.1
File Interpreter ABI Platform
pytest_git_selector-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.2 kB

Release files / pytest-git-selector-0.0.1.tar.gz

Download URL pytest-git-selector-0.0.1.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
09fe8feebc8074ae9408c999ffe994c038d7ef254ea6938a658e72bae42c5b8e
BLAKE2b-256 checksum
How to use checksums
226a56a41a663087c0091c7f4475c80e81ac8800165a9612210146e327219637
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release files / pytest_git_selector-0.0.1-py3-none-any.whl

Download URL pytest_git_selector-0.0.1-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
346ff3f43d1c97116aeec424dd3e0e1476daef6375e9841850bc351558529325
BLAKE2b-256 checksum
How to use checksums
6f856f35b05122df6298c1b527f2c243cfeccab1c092f17cefa61f38bd3aaccf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release history Release notifications | RSS feed

This release

0.0.1 This release

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