Merge pull request #649 from pytest-dev/sourcery/master

Sourcery refactored master branch
This commit is contained in:
Alessio Bogon 2023-11-12 23:29:02 +01:00 committed by GitHub
commit cf679a9c60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 10 additions and 15 deletions

View File

@ -64,7 +64,7 @@ class LogBDDCucumberJSON:
result: dict[str, Any] = {} result: dict[str, Any] = {}
if report.passed or not step["failed"]: # ignore setup/teardown if report.passed or not step["failed"]: # ignore setup/teardown
result = {"status": "passed"} result = {"status": "passed"}
elif report.failed and step["failed"]: elif report.failed:
result = {"status": "failed", "error_message": str(report.longrepr) if error_message else ""} result = {"status": "failed", "error_message": str(report.longrepr) if error_message else ""}
elif report.skipped: elif report.skipped:
result = {"status": "skipped"} result = {"status": "skipped"}

View File

@ -179,8 +179,7 @@ def _show_missing_code_main(config: Config, session: Session) -> None:
features, scenarios, steps = parse_feature_files(config.option.features) features, scenarios, steps = parse_feature_files(config.option.features)
for item in session.items: for item in session.items:
scenario = getattr(item.obj, "__scenario__", None) if scenario := getattr(item.obj, "__scenario__", None):
if scenario:
if scenario in scenarios: if scenario in scenarios:
scenarios.remove(scenario) scenarios.remove(scenario)
for step in scenario.steps: for step in scenario.steps:

View File

@ -90,7 +90,7 @@ class GherkinTerminalReporter(TerminalReporter):
self._tw.write("\n") self._tw.write("\n")
for step in report.scenario["steps"]: for step in report.scenario["steps"]:
self._tw.write(f" {step['keyword']} {step['name']}\n", **scenario_markup) self._tw.write(f" {step['keyword']} {step['name']}\n", **scenario_markup)
self._tw.write(" " + word, **word_markup) self._tw.write(f" {word}", **word_markup)
self._tw.write("\n\n") self._tw.write("\n\n")
self.stats.setdefault(cat, []).append(rep) self.stats.setdefault(cat, []).append(rep)

View File

@ -65,8 +65,7 @@ def strip_comments(line: str) -> str:
:return: Stripped line. :return: Stripped line.
""" """
res = COMMENT_RE.search(line) if res := COMMENT_RE.search(line):
if res:
line = line[: res.start()] line = line[: res.start()]
return line.strip() return line.strip()
@ -189,7 +188,7 @@ def parse_feature(basedir: str, filename: str, encoding: str = "utf-8") -> Featu
scenario.examples.set_param_names([l for l in split_line(parsed_line) if l]) scenario.examples.set_param_names([l for l in split_line(parsed_line) if l])
mode = types.EXAMPLE_LINE mode = types.EXAMPLE_LINE
elif mode == types.EXAMPLE_LINE: elif mode == types.EXAMPLE_LINE:
scenario.examples.add_example([l for l in split_line(stripped_line)]) scenario.examples.add_example(list(split_line(stripped_line)))
elif mode and mode not in (types.FEATURE, types.TAG): elif mode and mode not in (types.FEATURE, types.TAG):
step = Step(name=parsed_line, type=mode, indent=line_indent, line_number=line_number, keyword=keyword) step = Step(name=parsed_line, type=mode, indent=line_indent, line_number=line_number, keyword=keyword)
if feature.background and not scenario: if feature.background and not scenario:

View File

@ -46,7 +46,7 @@ def find_fixturedefs_for_step(step: Step, fixturemanager: FixtureManager, nodeid
"""Find the fixture defs that can parse a step.""" """Find the fixture defs that can parse a step."""
# happens to be that _arg2fixturedefs is changed during the iteration so we use a copy # happens to be that _arg2fixturedefs is changed during the iteration so we use a copy
fixture_def_by_name = list(fixturemanager._arg2fixturedefs.items()) fixture_def_by_name = list(fixturemanager._arg2fixturedefs.items())
for i, (fixturename, fixturedefs) in enumerate(fixture_def_by_name): for fixturename, fixturedefs in fixture_def_by_name:
for pos, fixturedef in enumerate(fixturedefs): for pos, fixturedef in enumerate(fixturedefs):
step_func_context = getattr(fixturedef.func, "_pytest_bdd_step_context", None) step_func_context = getattr(fixturedef.func, "_pytest_bdd_step_context", None)
if step_func_context is None: if step_func_context is None:
@ -244,13 +244,10 @@ def _get_scenario_decorator(
def collect_example_parametrizations( def collect_example_parametrizations(
templated_scenario: ScenarioTemplate, templated_scenario: ScenarioTemplate,
) -> list[ParameterSet] | None: ) -> list[ParameterSet] | None:
# We need to evaluate these iterators and store them as lists, otherwise if contexts := list(templated_scenario.examples.as_contexts()):
# we won't be able to do the cartesian product later (the second iterator will be consumed)
contexts = list(templated_scenario.examples.as_contexts())
if not contexts:
return None
return [pytest.param(context, id="-".join(context.values())) for context in contexts] return [pytest.param(context, id="-".join(context.values())) for context in contexts]
else:
return None
def scenario( def scenario(
@ -263,7 +260,7 @@ def scenario(
:param str encoding: Feature file encoding. :param str encoding: Feature file encoding.
""" """
__tracebackhide__ = True __tracebackhide__ = True
scenario_name = str(scenario_name) scenario_name = scenario_name
caller_module_path = get_caller_module_path() caller_module_path = get_caller_module_path()
# Get the feature # Get the feature