Skip to content

Commit 8415349

Browse files
committed
Merge remote-tracking branch 'origin/master' into updates
2 parents cbbb3d2 + a860eaf commit 8415349

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/pytest_bdd/parser.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import typing
77
from collections import OrderedDict
88
from dataclasses import dataclass, field
9+
from functools import cached_property
910
from typing import cast
1011

1112
from . import exceptions, types
@@ -318,9 +319,10 @@ def add_line(self, line: str) -> None:
318319
:param str line: Line of text - the continuation of the step name.
319320
"""
320321
self.lines.append(line)
322+
self._invalidate_full_name_cache()
321323

322-
@property
323-
def name(self) -> str:
324+
@cached_property
325+
def full_name(self) -> str:
324326
multilines_content = textwrap.dedent("\n".join(self.lines)) if self.lines else ""
325327

326328
# Remove the multiline quotes, if present.
@@ -334,9 +336,19 @@ def name(self) -> str:
334336
lines = [self._name] + [multilines_content]
335337
return "\n".join(lines).strip()
336338

339+
def _invalidate_full_name_cache(self) -> None:
340+
"""Invalidate the full_name cache."""
341+
if "full_name" in self.__dict__:
342+
del self.full_name
343+
344+
@property
345+
def name(self) -> str:
346+
return self.full_name
347+
337348
@name.setter
338349
def name(self, value: str) -> None:
339350
self._name = value
351+
self._invalidate_full_name_cache()
340352

341353
def __str__(self) -> str:
342354
"""Full step name including the type."""

tests/steps/test_common.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66

7-
from pytest_bdd import given, parsers, then, when
7+
from pytest_bdd import given, parser, parsers, then, when
88
from pytest_bdd.utils import collect_dumped_objects
99

1010

@@ -316,3 +316,25 @@ def _(n):
316316

317317
objects = collect_dumped_objects(result)
318318
assert objects == ["foo", ("foo parametrized", 1), "foo", ("foo parametrized", 2), "foo", ("foo parametrized", 3)]
319+
320+
321+
def test_step_name_is_cached():
322+
"""Test that the step name is cached and not re-computed eache time."""
323+
step = parser.Step(name="step name", type="given", indent=8, line_number=3, keyword="Given")
324+
assert step.name == "step name"
325+
326+
# manipulate the step name directly and validate the cache value is still returned
327+
step._name = "incorrect step name"
328+
assert step.name == "step name"
329+
330+
# change the step name using the property and validate the cache has been invalidated
331+
step.name = "new step name"
332+
assert step.name == "new step name"
333+
334+
# manipulate the step lines and validate the cache value is still returned
335+
step.lines.append("step line 1")
336+
assert step.name == "new step name"
337+
338+
# add a step line and validate the cache has been invalidated
339+
step.add_line("step line 2")
340+
assert step.name == "new step name\nstep line 1\nstep line 2"

0 commit comments

Comments
 (0)