Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serial marker for tests #2269

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ repos:
- ansible-compat>=2.2.0
- ansible-core
- enrich
- filelock
- flaky
- pytest
- rich>=11.0.0
Expand All @@ -157,6 +158,7 @@ repos:
- ansible-core
- docutils
- enrich
- filelock
- flaky
- jsonschema>=4.9.0
- pytest
Expand Down
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ xfail_strict = true
markers =
eco: Tests effects on a set of 3rd party ansible repositories
formatting_fixtures: Test that regenerates and tests formatting fixtures (requires prettier on PATH)
serial: Run this test serially via filelock.
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,14 @@ docs =
yamllint >= 1.26.3
test =
coverage >= 6.3
tomli >= 2.0.0
filelock
flaky >= 3.7.0
psutil # soft-dep of pytest-xdist
pytest >= 6.0.1
pytest-cov >= 2.10.1
pytest-plus >= 0.2 # for PYTEST_REQPASS
pytest-xdist >= 2.1.0
psutil # soft-dep of pytest-xdist
tomli >= 2.0.0
black # IDE support
mypy # IDE support
pylint # IDE support
Expand Down
18 changes: 17 additions & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""PyTest fixtures for testing the project."""
import os
from contextlib import contextmanager
from typing import TYPE_CHECKING, Iterator
from typing import TYPE_CHECKING, Generator, Iterator

import pytest
from _pytest.fixtures import FixtureRequest
from filelock import FileLock

if TYPE_CHECKING:
from typing import List # pylint: disable=ungrouped-imports
Expand Down Expand Up @@ -50,3 +52,17 @@ def pytest_collection_modifyitems(items: "List[nodes.Item]", config: "Config") -
item.add_marker(skip_other)
elif not do_regenerate and "formatting_fixtures" in item.keywords:
item.add_marker(skip_formatting_fixture)


@pytest.fixture(autouse=True)
def _block_on_serial_mark(request: FixtureRequest) -> Generator[None, None, None]:
"""Ensure that tests with serial marker do not run at the same time."""
# https://github.com/pytest-dev/pytest-xdist/issues/84
# https://github.com/pytest-dev/pytest-xdist/issues/385
os.makedirs(".tox", exist_ok=True)
if request.node.get_closest_marker("serial"):
# pylint: disable=abstract-class-instantiated
with FileLock(".tox/semaphore.lock"):
yield
else:
yield
1 change: 1 addition & 0 deletions test/test_cli_role_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def test_run_inside_role_dir(local_test_dir: str) -> None:
assert "Use shell only when shell functionality is required" in result.stdout


@pytest.mark.serial()
def test_run_role_three_dir_deep(local_test_dir: str) -> None:
"""Tests execution from deep inside a role."""
cwd = local_test_dir
Expand Down
1 change: 1 addition & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ def test_logger_debug(caplog: LogCaptureFixture) -> None:
assert expected_info in caplog.record_tuples


@pytest.mark.serial()
def test_cli_auto_detect(capfd: CaptureFixture[str]) -> None:
"""Test that run without arguments it will detect and lint the entire repository."""
cmd = [
Expand Down