Skip to main content

pytest-sherlock

Build Status Cov PyPI PyPI - Python Version PyPI - Wheel

Pytest plugin which help to find coupled tests.

Sometimes we have coupled tests which depend from ordering

For example:

  • PASSES tests/exmaple/test_all_read.py tests/exmaple/test_b_modify.py tests/exmaple/test_c_delete.py
  • FAILED tests/exmaple/test_c_delete.py tests/exmaple/test_b_modify.py tests/exmaple/test_all_read.py

In this case pretty simple to detect coupled tests, but if we have >=1k tests which called before it will hard

Content:

Install

pip install pytest-sherlock

how to use:

pytest tests/exmaple/test_c_delete.py tests/exmaple/test_b_modify.py tests/exmaple/test_all_read.py --flaky-test="test_read_params" -vv

Plugin didn't run all tests, it try to find some possible guilty test and will run first

Try to find coupled tests in [3-4] steps
__________________________ Step [1 of 4]: __________________________

tests/exmaple/test_c_delete.py::test_delete_random_param PASSED                                                                                                      [ 20%]
tests/exmaple/test_b_modify.py::test_modify_random_param PASSED                                                                                                      [ 40%]
tests/exmaple/test_c_delete.py::test_deleted_passed PASSED                                                                                                           [ 60%]
tests/exmaple/test_c_delete.py::test_do_not_delete PASSED                                                                                                            [ 80%]
tests/exmaple/test_all_read.py::test_read_params FAILED                                                                                                             [100%]
__________________________ Step [2 of 4]: __________________________

tests/exmaple/test_c_delete.py::test_delete_random_param PASSED                                                                                                      [ 33%]
tests/exmaple/test_b_modify.py::test_modify_random_param PASSED                                                                                                      [ 66%]
tests/exmaple/test_all_read.py::test_read_params FAILED                                                                                                             [100%]
__________________________ Step [3 of 4]: __________________________

tests/exmaple/test_c_delete.py::test_delete_random_param PASSED                                                                                                      [ 50%]
tests/exmaple/test_all_read.py::test_read_params PASSED                                                                                                              [100%]
__________________________ Step [4 of 4]: __________________________

tests/exmaple/test_b_modify.py::test_modify_random_param PASSED                                                                                                      [ 50%]
tests/exmaple/test_all_read.py::test_read_params FAILED                                                                                                             [100%]

============================== FAILURES ==============================
__________________________ test_read_params __________________________

Found coupled tests:
tests/exmaple/test_b_modify.py::test_modify_random_param
tests/exmaple/test_all_read.py::test_read_params

Common fixtures:
config

How to reproduce:
pytest -l -vv tests/exmaple/test_b_modify.py::test_modify_random_param tests/exmaple/test_all_read.py::test_read_params


AssertionError: assert 13 == 2
 +  where 13 = <built-in method get of dict object at 0x102664280>('b')
 +    where <built-in method get of dict object at 0x102664280> = {'a': 1, 'b': 13, 'c': 3}.get

tests/exmaple/test_all_read.py:8: AssertionError
=================== 1 failed, 9 passed in 0.08 seconds ===================

The plugin also supports --lf (last failed) option to rerun found coupled tests. It allows us to make sure that coupled tests are true.

pytest tests/exmaple/test_c_delete.py tests/exmaple/test_b_modify.py tests/exmaple/test_all_read.py --lf
=========================================================================== test session starts ============================================================================
platform darwin -- Python 3.7.10, pytest-3.5.1, py-1.7.0, pluggy-0.6.0
collected 10 items / 8 deselected
run-last-failure: rerun previous 2 failures

tests/exmaple/test_b_modify.py .                                                                                                                                     [ 50%]
tests/exmaple/test_all_read.py F                                                                                                                                     [100%]

================================================================================= FAILURES =================================================================================
_____________________________________________________________________________ test_read_params _____________________________________________________________________________

config = {'a': 1, 'b': 13, 'c': 3}, param = 'b'

    def test_read_params(config, param):
>       assert config.get(param) == 2
E       AssertionError: assert 13 == 2
E        +  where 13 = <built-in method get of dict object at 0x10c964cd0>('b')
E        +    where <built-in method get of dict object at 0x10c964cd0> = {'a': 1, 'b': 13, 'c': 3}.get

tests/exmaple/test_all_read.py:6: AssertionError
============================================================= 1 failed, 1 passed, 8 deselected in 0.05 seconds =============================================================

TODO

I have a couple ideas, how to improve finder coupled tests:

  • use AST for detect common peace of code (variables, functions, etc...)

Metadata

Release files for pytest-sherlock 0.3.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-sherlock 0.3.1
File Size Uploaded
pytest-sherlock-0.3.1.tar.gz 17.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-sherlock 0.3.1
File Interpreter ABI Platform
pytest_sherlock-0.3.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 28.6 kB

Release files / pytest-sherlock-0.3.1.tar.gz

Download URL pytest-sherlock-0.3.1.tar.gz
Size 17.6 kB
Tags Source
SHA-256 checksum
How to use checksums
a9f4821aeeefd37eff81ed1d46af0510e1fe7b24aef9febf1fdaf20d2cc3b108
BLAKE2b-256 checksum
How to use checksums
3b2b40aa8fd20ccf1b64b0a71e8e5b6dbdd7de72ba5c33a60b937c9bfd4ecf9f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.15

Release files / pytest_sherlock-0.3.1-py2.py3-none-any.whl

Download URL pytest_sherlock-0.3.1-py2.py3-none-any.whl
Size 11.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
e1dc7849815f257de779a4f1a3215f437bcb2b8afd25517ccffa2b0989bc7061
BLAKE2b-256 checksum
How to use checksums
14b07dc7ab83f6d28d4e22a3619f348cd62aa12cc4fcc7d885b726770be5c76e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.7.15

Release history Release notifications | RSS feed

0.3.4

2 release files

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.4

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

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