Skip to content

Commit 5f08065

Browse files
LouisTsai-Csiemarioevz
authored andcommitted
refactor: rename state to stateful marker
1 parent fec3001 commit 5f08065

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
@@ -431,7 +431,7 @@ def create_function_page_props(self, test_functions: Dict["str", List[Item]]) ->
431431
)
432432

433433
is_benchmark = items[0].get_closest_marker("benchmark") is not None
434-
is_state = items[0].get_closest_marker("state") is not None
434+
is_stateful = items[0].get_closest_marker("stateful") is not None
435435

436436
self.function_page_props[function_id] = FunctionPageProps(
437437
title=get_test_function_name(items[0]),
@@ -448,7 +448,7 @@ def create_function_page_props(self, test_functions: Dict["str", List[Item]]) ->
448448
html_static_page_target=f"./{get_test_function_name(items[0])}.html",
449449
mkdocs_function_page_target=f"./{get_test_function_name(items[0])}/",
450450
is_benchmark=is_benchmark,
451-
is_state=is_state,
451+
is_stateful=is_stateful,
452452
)
453453

454454
def create_module_page_props(self) -> None:
@@ -464,7 +464,7 @@ def create_module_page_props(self) -> None:
464464
pytest_node_id=str(module_path),
465465
package_name=get_import_path(module_path),
466466
is_benchmark=function_page.is_benchmark,
467-
is_state=function_page.is_state,
467+
is_stateful=function_page.is_stateful,
468468
test_functions=[
469469
TestFunction(
470470
name=function_page.title,
@@ -478,8 +478,8 @@ def create_module_page_props(self) -> None:
478478
existing_module_page = self.module_page_props[str(function_page.path)]
479479
if function_page.is_benchmark:
480480
existing_module_page.is_benchmark = True
481-
if function_page.is_state:
482-
existing_module_page.is_state = True
481+
if function_page.is_stateful:
482+
existing_module_page.is_stateful = True
483483
existing_module_page.test_functions.append(
484484
TestFunction(
485485
name=function_page.title,
@@ -518,8 +518,8 @@ def add_directory_page_props(self) -> None:
518518
for module_page in self.module_page_props.values()
519519
if directory in module_page.path.parents or module_page.path.parent == directory
520520
)
521-
is_state = any(
522-
module_page.is_state
521+
is_stateful = any(
522+
module_page.is_stateful
523523
for module_page in self.module_page_props.values()
524524
if directory in module_page.path.parents or module_page.path.parent == directory
525525
)
@@ -536,7 +536,7 @@ def add_directory_page_props(self) -> None:
536536
# init.py will be used for docstrings
537537
package_name=get_import_path(directory),
538538
is_benchmark=is_benchmark,
539-
is_state=is_state,
539+
is_stateful=is_stateful,
540540
)
541541

542542
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
@@ -111,7 +111,7 @@ class PagePropsBase:
111111
pytest_node_id: str
112112
package_name: str
113113
is_benchmark: bool = False
114-
is_state: bool = False
114+
is_stateful: bool = False
115115

116116
@property
117117
@abstractmethod

src/pytest_plugins/shared/execute_fill.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def pytest_configure(config: pytest.Config):
102102
)
103103
config.addinivalue_line(
104104
"markers",
105-
"state: Tests for stateful benchmarking scenarios.",
105+
"stateful: Tests for stateful benchmarking scenarios.",
106106
)
107107
config.addinivalue_line(
108108
"markers",

tests/benchmark/conftest.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def pytest_collection_modifyitems(config, items):
4141
if (
4242
benchmark_dir in Path(item.fspath).parents
4343
and not item.get_closest_marker("benchmark")
44-
and not item.get_closest_marker("state")
44+
and not item.get_closest_marker("stateful")
4545
):
4646
item.add_marker(benchmark_marker)
4747
return
@@ -50,14 +50,16 @@ def pytest_collection_modifyitems(config, items):
5050
run_benchmarks = (
5151
marker_expr and "benchmark" in marker_expr and "not benchmark" not in marker_expr
5252
)
53-
run_state_tests = marker_expr and "state" in marker_expr and "not state" not in marker_expr
53+
run_stateful_tests = (
54+
marker_expr and "stateful" in marker_expr and "not stateful" not in marker_expr
55+
)
5456

5557
items_for_removal = []
5658
for i, item in enumerate(items):
5759
is_in_benchmark_dir = benchmark_dir in Path(item.fspath).parents
58-
has_state_marker = item.get_closest_marker("state")
60+
has_stateful_marker = item.get_closest_marker("stateful")
5961
is_benchmark_test = (
60-
is_in_benchmark_dir and not has_state_marker
62+
is_in_benchmark_dir and not has_stateful_marker
6163
) or item.get_closest_marker("benchmark")
6264

6365
if is_benchmark_test:
@@ -67,7 +69,7 @@ def pytest_collection_modifyitems(config, items):
6769
items_for_removal.append(i)
6870
elif run_benchmarks:
6971
items_for_removal.append(i)
70-
elif is_in_benchmark_dir and has_state_marker and not run_state_tests:
72+
elif is_in_benchmark_dir and has_stateful_marker and not run_stateful_tests:
7173
items_for_removal.append(i)
7274

7375
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)