Skip to content

Commit 5e1a717

Browse files
refactor: rename state to stateful marker
1 parent a06bd60 commit 5e1a717

File tree

9 files changed

+90
-17
lines changed

9 files changed

+90
-17
lines changed

docs/templates/base.md.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ Documentation for [`{{ pytest_node_id }}@{{ short_git_ref }}`]({{ source_code_ur
1111
```console
1212
{% if is_benchmark %}
1313
fill -v {{ pytest_node_id }} -m benchmark
14-
{% elif is_state %}
15-
fill -v {{ pytest_node_id }} -m state
14+
{% elif is_stateful %}
15+
fill -v {{ pytest_node_id }} -m stateful
1616
{% else %}
1717
fill -v {{ pytest_node_id }} --fork {{ target_or_valid_fork }}
1818
{% endif %}

src/pytest_plugins/filler/gen_test_doc/gen_test_doc.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ def create_function_page_props(self, test_functions: Dict["str", List[Item]]) ->
424424
)
425425

426426
is_benchmark = items[0].get_closest_marker("benchmark") is not None
427-
is_state = items[0].get_closest_marker("state") is not None
427+
is_stateful = items[0].get_closest_marker("stateful") is not None
428428

429429
self.function_page_props[function_id] = FunctionPageProps(
430430
title=get_test_function_name(items[0]),
@@ -441,7 +441,7 @@ def create_function_page_props(self, test_functions: Dict["str", List[Item]]) ->
441441
html_static_page_target=f"./{get_test_function_name(items[0])}.html",
442442
mkdocs_function_page_target=f"./{get_test_function_name(items[0])}/",
443443
is_benchmark=is_benchmark,
444-
is_state=is_state,
444+
is_stateful=is_stateful,
445445
)
446446

447447
def create_module_page_props(self) -> None:
@@ -457,7 +457,7 @@ def create_module_page_props(self) -> None:
457457
pytest_node_id=str(module_path),
458458
package_name=get_import_path(module_path),
459459
is_benchmark=function_page.is_benchmark,
460-
is_state=function_page.is_state,
460+
is_stateful=function_page.is_stateful,
461461
test_functions=[
462462
TestFunction(
463463
name=function_page.title,
@@ -471,8 +471,8 @@ def create_module_page_props(self) -> None:
471471
existing_module_page = self.module_page_props[str(function_page.path)]
472472
if function_page.is_benchmark:
473473
existing_module_page.is_benchmark = True
474-
if function_page.is_state:
475-
existing_module_page.is_state = True
474+
if function_page.is_stateful:
475+
existing_module_page.is_stateful = True
476476
existing_module_page.test_functions.append(
477477
TestFunction(
478478
name=function_page.title,
@@ -510,8 +510,8 @@ def add_directory_page_props(self) -> None:
510510
for module_page in self.module_page_props.values()
511511
if directory in module_page.path.parents or module_page.path.parent == directory
512512
)
513-
is_state = any(
514-
module_page.is_state
513+
is_stateful = any(
514+
module_page.is_stateful
515515
for module_page in self.module_page_props.values()
516516
if directory in module_page.path.parents or module_page.path.parent == directory
517517
)
@@ -526,7 +526,7 @@ def add_directory_page_props(self) -> None:
526526
target_or_valid_fork=fork.capitalize() if fork else "Unknown",
527527
package_name=get_import_path(directory), # init.py will be used for docstrings
528528
is_benchmark=is_benchmark,
529-
is_state=is_state,
529+
is_stateful=is_stateful,
530530
)
531531

532532
def find_files_within_collection_scope(self, file_pattern: str) -> List[Path]:

src/pytest_plugins/filler/gen_test_doc/page_props.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class PagePropsBase:
105105
pytest_node_id: str
106106
package_name: str
107107
is_benchmark: bool = False
108-
is_state: bool = False
108+
is_stateful: bool = False
109109

110110
@property
111111
@abstractmethod

src/pytest_plugins/shared/execute_fill.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def pytest_configure(config: pytest.Config):
9898
)
9999
config.addinivalue_line(
100100
"markers",
101-
"state: Tests for stateful benchmarking scenarios.",
101+
"stateful: Tests for stateful benchmarking scenarios.",
102102
)
103103
config.addinivalue_line(
104104
"markers",

tests/benchmark/conftest.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def pytest_collection_modifyitems(config, items):
3636
if (
3737
benchmark_dir in Path(item.fspath).parents
3838
and not item.get_closest_marker("benchmark")
39-
and not item.get_closest_marker("state")
39+
and not item.get_closest_marker("stateful")
4040
):
4141
item.add_marker(benchmark_marker)
4242
return
@@ -45,14 +45,16 @@ def pytest_collection_modifyitems(config, items):
4545
run_benchmarks = (
4646
marker_expr and "benchmark" in marker_expr and "not benchmark" not in marker_expr
4747
)
48-
run_state_tests = marker_expr and "state" in marker_expr and "not state" not in marker_expr
48+
run_stateful_tests = (
49+
marker_expr and "stateful" in marker_expr and "not stateful" not in marker_expr
50+
)
4951

5052
items_for_removal = []
5153
for i, item in enumerate(items):
5254
is_in_benchmark_dir = benchmark_dir in Path(item.fspath).parents
53-
has_state_marker = item.get_closest_marker("state")
55+
has_stateful_marker = item.get_closest_marker("stateful")
5456
is_benchmark_test = (
55-
is_in_benchmark_dir and not has_state_marker
57+
is_in_benchmark_dir and not has_stateful_marker
5658
) or item.get_closest_marker("benchmark")
5759

5860
if is_benchmark_test:
@@ -62,7 +64,7 @@ def pytest_collection_modifyitems(config, items):
6264
items_for_removal.append(i)
6365
elif run_benchmarks:
6466
items_for_removal.append(i)
65-
elif is_in_benchmark_dir and has_state_marker and not run_state_tests:
67+
elif is_in_benchmark_dir and has_stateful_marker and not run_stateful_tests:
6668
items_for_removal.append(i)
6769

6870
for i in reversed(items_for_removal):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Benchmark state tests package."""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Bloatnet benchmark tests package."""
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
abstract: Tests benchmark worst-case bloatnet scenarios.
3+
Tests benchmark worst-case bloatnet scenarios.
4+
5+
Tests running worst-case bloatnet scenarios for benchmarking purposes.
6+
"""
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""Pytest configuration for state tests."""
2+
3+
from pathlib import Path
4+
5+
import pytest
6+
7+
DEFAULT_BENCHMARK_FORK = "Prague"
8+
9+
10+
def pytest_generate_tests(metafunc):
11+
"""Add default valid_from marker to state tests without explicit fork specification."""
12+
state_dir = Path(__file__).parent
13+
test_file_path = Path(metafunc.definition.fspath)
14+
15+
if state_dir in test_file_path.parents:
16+
has_valid_from = any(
17+
marker.name == "valid_from" for marker in metafunc.definition.iter_markers()
18+
)
19+
if not has_valid_from:
20+
metafunc.definition.add_marker(pytest.mark.valid_from(DEFAULT_BENCHMARK_FORK))
21+
22+
23+
def pytest_collection_modifyitems(config, items):
24+
"""Manage stateful test markers and filtering."""
25+
state_dir = Path(__file__).parent
26+
gen_docs = config.getoption("--gen-docs", default=False)
27+
28+
if gen_docs:
29+
_add_stateful_markers_for_docs(items, state_dir)
30+
return
31+
32+
marker_expr = config.getoption("-m", default="")
33+
34+
items_to_remove = []
35+
36+
for i, item in enumerate(items):
37+
item_path = Path(item.fspath)
38+
is_in_state_dir = state_dir in item_path.parents
39+
40+
# Add stateful marker to tests in state directory that don't have it
41+
if is_in_state_dir and not item.get_closest_marker("stateful"):
42+
item.add_marker(pytest.mark.stateful)
43+
44+
has_stateful_marker = item.get_closest_marker("stateful")
45+
46+
run_stateful = (
47+
marker_expr and ("stateful" in marker_expr) and ("not stateful" not in marker_expr)
48+
)
49+
50+
# When not running stateful tests, remove all stateful tests
51+
if not run_stateful and has_stateful_marker:
52+
items_to_remove.append(i)
53+
54+
for i in reversed(items_to_remove):
55+
items.pop(i)
56+
57+
58+
def _add_stateful_markers_for_docs(items, state_dir):
59+
"""Add stateful markers for documentation generation."""
60+
for item in items:
61+
item_path = Path(item.fspath)
62+
if state_dir in item_path.parents and not item.get_closest_marker("stateful"):
63+
item.add_marker(pytest.mark.stateful)

0 commit comments

Comments
 (0)