Package: pytest-git-selector
pytest-git-selectorIntroductionpytest-git-selectoris 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).IdeaThe idea is fairly simple:Changes are analyzed using agit diffcommand. The arguments togit diffare supplied by the userIf a test module contains one of the changed files in its import tree, then select that test moduleThe import tree is created usingimportlabwhich statically analyzes import statements to determine dependencies.See thegit-diff documentationfor arguments that can be supplied togit diff.pytestpluginThe plugin will automatically deselect tests whose descendants in the dependency tree have not changed. The changed are determined via agit diffcommand that is called with user supplied argument. User supplied arguments togit diffmust be appear at the end of thepytestarguments separated from the rest of thepytestarguments using the--delimiter e.g.pytest --collect-only -- --diff-filter=M HEAD~1...This plugin adds two additional flags topytest:--src-path- specifies the directory containing the source code for the project.srcand the current working directory are automatically added so this argument should not be required in most cases--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.ExamplesThe following examples assume the project contains source code in<project_root>/src/and tests in<project_root>/test/.pytestis run in<project_root>in all the following examples.Selecting tests affected by changes between a feature branch and its merge basepytest -- main...Selecting tests affected by changes in the last commitpytest -- HEAD~1...Selecting tests affected by changes between two commits with an extra dependency fileextra_deps.txtspecified and source code incustom_src_dir/pytest --src-path custom_src_dir/ --extra-deps-file extra_deps.txt -- 1b23b4b1...12da93k8Command line toolThegit-select-testscommand line tool can be used to print out selected test modules. This is mainly useful for projects that do not usepytestas a test runner e.g.nosetest. The arguments to thegit diffcommand are separated by the--delimiter. Seegit-select-tests --helpfor usage.ExamplesThe 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 basegit-select-tests --src-path src/ --test-path test/ -- main...Selecting tests affected by changes in the last commitgit-select-tests --src-path src/ --test-path test/ -- HEAD~1...Comparison withpytest-diff-selectorThis idea has been implemented before in thispytest-diff-selectorproject. The main differences are:pytest-git-selectorleverages theimportlablibrary as opposed to using a proprietary static analyzerpytest-git-selectorimplements apytestpluginCurrentlypytest-diff-selectordoes not appear to be working and has not been updated recently which is the reason for this project's existence.