diff --git a/pyproject.toml b/pyproject.toml index 28ba6f76..048d10ff 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -258,7 +258,7 @@ score = "no" max-line-length = 200 [tool.pytest.ini_options] -addopts = '-p no:robotframework' +addopts = ['-p no:robotframework', '--import-mode=importlib'] xfail_strict = true [tool.mypy] diff --git a/tests/test_python.py b/tests/test_python.py index 02f2b0f8..a367a480 100644 --- a/tests/test_python.py +++ b/tests/test_python.py @@ -514,5 +514,32 @@ def test_foo(): BuiltIn().run_keyword('bar') """ ) - run_and_assert_result(pytester, passed=1) + run_and_assert_result(pytester, errors=1, pytest_args=["--import-mode=importlib"]) + # we add the current directory to the syspath to simulate `py -m pytest` + pytester.syspathinsert(".") + run_and_assert_result(pytester, passed=1, pytest_args=["--import-mode=importlib"]) + assert_log_file_exists(pytester) + + +def test_robot_keyword_in_python_test_prepend(pytester: Pytester): + pytester.makefile( + ".resource", + bar=""" + *** keywords *** + bar + log 1 + """, + ) + pytester.makepyfile( # type:ignore[no-untyped-call] + """ + from pytest_robotframework import import_resource + from robot.libraries.BuiltIn import BuiltIn + + import_resource("bar.resource") + + def test_foo(): + BuiltIn().run_keyword('bar') + """ + ) + run_and_assert_result(pytester, passed=1, pytest_args=["--import-mode=prepend"]) assert_log_file_exists(pytester) diff --git a/tests/utils.py b/tests/utils.py index 765af3a9..67f3a485 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -31,11 +31,15 @@ def assert_log_file_exists(pytester: Pytester): def run_pytest( pytester: Pytester, *pytest_args: str, plugins: list[object] | None = None ) -> RunResult: + __traceback_hide__ = True # TODO: figure out why robot doesn't use pytester's cd anymore. started happening when # i added a test that calls a function from the plugin directly instead of using pytester # https://github.com/DetachHead/pytest-robotframework/issues/38 return pytester.runpytest( - *pytest_args, "--robotargs", f"-d {pytester.path}", plugins=plugins or [] + *pytest_args, + "--robotargs", + f"-d {pytester.path}", + plugins=plugins or [], ) @@ -48,6 +52,7 @@ def run_and_assert_result( failed=0, errors=0, ): + __traceback_hide__ = True result = run_pytest(pytester, *(pytest_args or [])) result.assert_outcomes(passed=passed, skipped=skipped, failed=failed, errors=errors) assert_robot_total_stats(