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

Allow a class to declare several scenarios #3757

Merged
merged 2 commits into from
Dec 31, 2024
Merged
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
13 changes: 8 additions & 5 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,23 +267,26 @@ def pytest_collection_modifyitems(session, config, items: list[pytest.Item]):
selected = []
deselected = []

declared_scenarios = {}
all_declared_scenarios = {}

def iter_markers(self, name=None):
return (x[1] for x in self.iter_markers_with_node(name=name) if x[1].name not in ("skip", "skipif", "xfail"))

for item in items:
scenario_markers = list(item.iter_markers("scenario"))
declared_scenario = scenario_markers[0].args[0] if len(scenario_markers) != 0 else "DEFAULT"
if len(scenario_markers) == 0:
declared_scenarios = ["DEFAULT"]
else:
declared_scenarios = [marker.args[0] for marker in scenario_markers]

declared_scenarios[item.nodeid] = declared_scenario
all_declared_scenarios[item.nodeid] = declared_scenarios

# If we are running scenario with the option sleep, we deselect all
if session.config.option.sleep or session.config.option.vm_gitlab_pipeline:
deselected.append(item)
continue

if context.scenario.name == declared_scenario:
if context.scenario.name in declared_scenarios:
logger.info(f"{item.nodeid} is included in {context.scenario}")
selected.append(item)

Expand All @@ -304,7 +307,7 @@ def iter_markers(self, name=None):

if config.option.scenario_report:
with open(f"{context.scenario.host_log_folder}/scenarios.json", "w", encoding="utf-8") as f:
json.dump(declared_scenarios, f, indent=2)
json.dump(all_declared_scenarios, f, indent=2)


def pytest_deselected(items):
Expand Down
13 changes: 5 additions & 8 deletions tests/test_the_test/test_scenario_decorator.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import pytest
from utils import scenarios


@scenarios.test_the_test
class Test_Decorator:
def test_uniqueness(self):
with pytest.raises(ValueError):

@scenarios.integrations
@scenarios.apm_tracing_e2e
class Test_Dbm:
pass
def test_allow_several(self):
@scenarios.integrations
@scenarios.apm_tracing_e2e
class Test_Dbm:
pass
5 changes: 0 additions & 5 deletions utils/_context/_scenarios/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ def _create_log_subfolder(self, subfolder, *, remove_if_exists=False):
def __call__(self, test_object):
"""Handles @scenarios.scenario_name"""

# Check that no scenario has been already declared
for marker in getattr(test_object, "pytestmark", []):
if marker.name == "scenario":
raise ValueError(f"Error on {test_object}: You can declare only one scenario")

pytest.mark.scenario(self.name)(test_object)

return test_object
Expand Down
9 changes: 5 additions & 4 deletions utils/scripts/compute_impacted_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def main():
# this file is generated with
# ./run.sh MOCK_THE_TEST --collect-only --scenario-report
with open("logs_mock_the_test/scenarios.json", encoding="utf-8") as f:
scenario_map = json.load(f)
scenario_map: dict[str, list[str]] = json.load(f)

modified_nodeids = set()

Expand All @@ -83,13 +83,14 @@ def main():
modified_nodeids.add(nodeid)

scenarios_by_files = defaultdict(set)
for nodeid in scenario_map:
for nodeid, scenarios in scenario_map.items():
file = nodeid.split(":", 1)[0]
scenarios_by_files[file].add(scenario_map[nodeid])
for scenario in scenarios:
scenarios_by_files[file].add(scenario)

for modified_nodeid in modified_nodeids:
if nodeid.startswith(modified_nodeid):
result.add_scenario(scenario_map[nodeid])
result.add_scenarios(scenarios)
break

# this file is generated with
Expand Down
Loading