Skip to content

Commit

Permalink
Merge pull request #85 from maratori/small-tests
Browse files Browse the repository at this point in the history
tests: split long tests to class methods
  • Loading branch information
fscherf committed Sep 17, 2021
2 parents 4438965 + 9950e94 commit 1f0b6b1
Show file tree
Hide file tree
Showing 5 changed files with 571 additions and 396 deletions.
4 changes: 3 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[pytest]
addopts= tests -v -rsx --tb=long --cov-report=xml --cov-report=html:htmlcov --cov-report=term:skip-covered
addopts= tests -v -rsx --tb=long --strict-markers --cov-report=xml --cov-report=html:htmlcov --cov-report=term:skip-covered
markers =
incremental: marks test class to stop execution after the first test method failed
log_cli=false
log_level=NOTSET
log_format = %(levelname)-8s %(name)-30s [%(asctime)s.%(msecs)03d] %(message)s
Expand Down
56 changes: 56 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from typing import Dict, Tuple
import pytest

# see https://docs.pytest.org/en/latest/example/simple.html#incremental-testing-test-steps # NOQA: E501

# store history of failures per test class name
# and per index in parametrize (if parametrize used)
_test_failed_incremental: Dict[str, Dict[Tuple[int, ...], str]] = {}


def pytest_runtest_makereport(item, call):
if 'incremental' in item.keywords:
# incremental marker is used
if call.excinfo is not None:
# the test has failed
# retrieve the class name of the test
cls_name = str(item.cls)
# retrieve the index of the test
# (if parametrize is used in combination with incremental)
parametrize_index = (
tuple(item.callspec.indices.values())
if hasattr(item, 'callspec')
else ()
)
# retrieve the name of the test function
test_name = item.originalname or item.name
# store in _test_failed_incremental
# the original name of the failed test
_test_failed_incremental.setdefault(cls_name, {}).setdefault(
parametrize_index, test_name
)


def pytest_runtest_setup(item):
if 'incremental' in item.keywords:
# retrieve the class name of the test
cls_name = str(item.cls)
# check if a previous test has failed for this class
if cls_name in _test_failed_incremental:
# retrieve the index of the test
# (if parametrize is used in combination with incremental)
parametrize_index = (
tuple(item.callspec.indices.values())
if hasattr(item, 'callspec')
else ()
)
# retrieve the name of the first test function
# to fail for this class name and index
test_name = _test_failed_incremental[cls_name].get(
parametrize_index,
None,
)
# if name found, test has failed
# for the combination of class name & test name
if test_name is not None:
pytest.xfail(f'previous test failed ({test_name})')

0 comments on commit 1f0b6b1

Please sign in to comment.