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

fix some issues with context manager keywords #86

Merged
merged 1 commit into from
Sep 15, 2023
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
5 changes: 4 additions & 1 deletion pytest_robotframework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ def __exit__(
suppress = self.wrapped.__exit__( # type:ignore[no-any-expr]
exc_type, exc_value, traceback
)
exit_status_reporter(self.status_reporter) # type:ignore[no-any-expr]
exit_status_reporter(
self.status_reporter, # type:ignore[no-any-expr]
(None if suppress else exc_value),
)
return suppress

@wraps(fn)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
@keyword
@contextmanager
def asdf() -> Iterator[None]:
raise Exception
logger.info("start") # type:ignore[no-untyped-call]
try:
yield
finally:
logger.info("end") # type:ignore[no-untyped-call]


# type tests
Expand All @@ -26,4 +30,6 @@ def asdf() -> Iterator[None]:

def test_foo():
with asdf():
logger.info(1) # type:ignore[no-untyped-call]
logger.info(0) # type:ignore[no-untyped-call]
raise Exception
logger.info(1) # type:ignore[unreachable]
19 changes: 19 additions & 0 deletions tests/fixtures/test_python/test_keyword_raises.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from __future__ import annotations

from robot.api import logger

from pytest_robotframework import keyword


class FooError(Exception):
pass


@keyword
def bar():
raise FooError


def test_foo():
bar()
logger.info(1) # type:ignore[no-untyped-call]
17 changes: 15 additions & 2 deletions tests/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,14 @@ def test_keyword_decorator_context_manager_that_doesnt_suppress(
):
run_and_assert_result(pytester_dir, failed=1)
assert_log_file_exists(pytester_dir)
assert output_xml(pytester_dir).xpath(
"//kw[@name='asdf' and ./status[@status='FAIL']]"
xml = output_xml(pytester_dir)
assert xml.xpath("//kw[@name='asdf']/msg[@level='INFO' and .='start']")
assert xml.xpath("//kw[@name='asdf']/msg[@level='INFO' and .='0']")
assert xml.xpath("//kw[@name='asdf']/msg[@level='INFO' and .='end']")
assert xml.xpath(
"//kw[@name='asdf' and ./status[@status='FAIL'] and ./msg[.='Exception']]"
)
assert not xml.xpath("//msg[.='1']")


def test_keywordify_keyword_inside_context_manager(pytester_dir: PytesterDir):
Expand Down Expand Up @@ -543,3 +548,11 @@ def test_keyword_and_pytest_raises(pytester_dir: PytesterDir):
assert output_xml(pytester_dir).xpath(
"//kw[@name='raises']/kw[@name='bar']/status[@status='FAIL']"
)


def test_keyword_raises(pytester_dir: PytesterDir):
run_and_assert_result(pytester_dir, failed=1)
assert_log_file_exists(pytester_dir)
assert output_xml(pytester_dir).xpath(
"//kw[@name='bar' and ./status[@status='FAIL'] and ./msg[.='FooError']]"
)