diff --git a/pytest_robotframework/__init__.py b/pytest_robotframework/__init__.py index 50f39ed9..dd71def0 100644 --- a/pytest_robotframework/__init__.py +++ b/pytest_robotframework/__init__.py @@ -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) diff --git a/tests/fixtures/test_python/test_keyword_decorator_context_manager_that_doesnt_suppress.py b/tests/fixtures/test_python/test_keyword_decorator_context_manager_that_doesnt_suppress.py index 683d5f4f..65201d62 100644 --- a/tests/fixtures/test_python/test_keyword_decorator_context_manager_that_doesnt_suppress.py +++ b/tests/fixtures/test_python/test_keyword_decorator_context_manager_that_doesnt_suppress.py @@ -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 @@ -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] diff --git a/tests/fixtures/test_python/test_keyword_raises.py b/tests/fixtures/test_python/test_keyword_raises.py new file mode 100644 index 00000000..fcec3cae --- /dev/null +++ b/tests/fixtures/test_python/test_keyword_raises.py @@ -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] diff --git a/tests/test_python.py b/tests/test_python.py index 64c92906..eab8c1e0 100644 --- a/tests/test_python.py +++ b/tests/test_python.py @@ -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): @@ -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']]" + )