Skip to content

Commit 8e60e03

Browse files
create integration test plugin for pytest
1 parent 75739d3 commit 8e60e03

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

tests/conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
from .settings import DEFAULT_SETTINGS
1818
from .utils import seq
1919

20-
pytest_plugins = ["tests.plugins.django_modeladmin"]
20+
pytest_plugins = [
21+
"tests.plugins.django_modeladmin",
22+
"tests.plugins.integration",
23+
]
2124

2225

2326
def pytest_configure(config):

tests/integration/__init__.py

Whitespace-only changes.

tests/integration/test_plugin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
5+
6+
def test_automatically_skip():
7+
pytest.fail("tests in `integration` directory should be automatically skipped")

tests/plugins/integration.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from __future__ import annotations
2+
3+
from pathlib import Path
4+
5+
import pytest
6+
7+
MARK = "integration"
8+
CLI_FLAG = "--integration"
9+
10+
11+
def pytest_addoption(parser: pytest.Parser) -> None:
12+
"""Add the --integration flag to pytest CLI.
13+
14+
Args:
15+
parser: pytest parser.
16+
"""
17+
parser.addoption(
18+
CLI_FLAG, action="store_true", help="run integration tests as well"
19+
)
20+
21+
22+
def pytest_configure(config: pytest.Config) -> None:
23+
"""Add the integration marker to pytest config.
24+
25+
Args:
26+
config: pytest config.
27+
"""
28+
config.addinivalue_line("markers", f"{MARK}: mark tests as integration tests")
29+
30+
31+
def pytest_runtest_setup(item: pytest.Item) -> None:
32+
"""Do not run tests marked as integration tests by default, unless a flag is set.
33+
34+
Args:
35+
item: pytest's test item.
36+
"""
37+
if MARK in item.keywords and not item.config.getoption(CLI_FLAG):
38+
pytest.skip(f"pass {CLI_FLAG} to run integration tests")
39+
40+
41+
def pytest_collection_modifyitems(
42+
config: pytest.Config, items: list[pytest.Item]
43+
) -> None:
44+
"""Marks tests in the integration folder as end-to-end tests (which will not run by default).
45+
46+
Args:
47+
config: pytest configuration.
48+
items: Items to test (individual tests).
49+
"""
50+
integration_dir = Path(__file__).parent.parent / "integration"
51+
for item in items:
52+
if Path(item.fspath).parent == integration_dir:
53+
item.add_marker(MARK)

0 commit comments

Comments
 (0)