Skip to content

Commit b0197df

Browse files
committed
fix keywords that get called from a .py file in a .robot test only appearing in the robot log on the first call
1 parent 97091c8 commit b0197df

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
lines changed

pytest_robotframework/_internal/robot/listeners_and_suite_visitors.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -656,22 +656,27 @@ class KeywordUnwrapper(ListenerV3):
656656
printed a second time since they would already have been printed in a child keyword
657657
"""
658658

659+
def __init__(self) -> None:
660+
self.__original_keyword_stack = list[Function | None]()
661+
659662
@override
660663
def start_library_keyword(
661664
self,
662665
data: running.Keyword,
663666
implementation: running.LibraryKeyword,
664667
result: result.Keyword, # pylint:disable=redefined-outer-name
665668
):
669+
"""replaces a decorated keyword for when it gets called from a `.robot` file"""
666670
if not isinstance(implementation, StaticKeyword):
667671
return
668-
original_function: Function | None = getattr(
672+
original_function = getattr(
669673
implementation.method, _keyword_original_function_attr, None
670674
)
671-
675+
self.__original_keyword_stack.append(
676+
getattr(implementation.owner.instance, implementation.method_name, None) # pyright:ignore[reportAny]
677+
)
672678
if original_function is None:
673679
return
674-
675680
setattr(
676681
implementation.owner.instance, # pyright:ignore[reportAny]
677682
implementation.method_name,
@@ -681,3 +686,25 @@ def start_library_keyword(
681686
else original_function
682687
),
683688
)
689+
690+
@override
691+
def end_library_keyword(
692+
self,
693+
data: running.Keyword,
694+
implementation: running.LibraryKeyword,
695+
result: result.Keyword, # pylint:disable=redefined-outer-name
696+
):
697+
"""
698+
after the keyword is finished, restore its original value. otherwise if it gets called
699+
again from a `.py` file, it won't show up as a keyword in the robot log.
700+
"""
701+
if not isinstance(implementation, StaticKeyword):
702+
return
703+
original_keyword = self.__original_keyword_stack.pop()
704+
if original_keyword is None:
705+
return
706+
setattr(
707+
implementation.owner.instance, # pyright:ignore[reportAny]
708+
implementation.method_name,
709+
original_keyword,
710+
)

tests/fixtures/test_robot/test_keyword_called_twice_in_robot_test/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*** Settings ***
2+
Library ./foo.py
3+
4+
5+
*** Test Cases ***
6+
Asdfasdf
7+
Bar
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from __future__ import annotations
2+
3+
from pytest_robotframework import keyword
4+
5+
6+
@keyword
7+
def foo(): ...
8+
9+
10+
@keyword
11+
def bar():
12+
foo()
13+
foo()

tests/test_robot.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,11 @@ def test_keyword_decorator_class_library(pr: PytestRobotTester):
342342
def test_pass_execution(pr: PytestRobotTester):
343343
pr.run_and_assert_result(passed=1)
344344
pr.assert_log_file_exists()
345+
346+
347+
def test_keyword_called_twice_in_robot_test(pr: PytestRobotTester):
348+
pr.run_and_assert_result(passed=1)
349+
pr.assert_log_file_exists()
350+
results = output_xml().xpath("//kw[@name='Run Test']/kw[@name='Bar']/kw[@name='Foo']")
351+
assert isinstance(results, list)
352+
assert len(results) == 2

0 commit comments

Comments
 (0)