From d124b2af7d6d8725dd93b418d5e22f6b1c7e6fda Mon Sep 17 00:00:00 2001 From: detachhead Date: Wed, 30 Aug 2023 10:58:50 +1000 Subject: [PATCH] fix collecting on nested suites not removing the fake test --- pytest_robotframework/_common.py | 31 +++++++++---------- .../asdf/__init__.py | 0 .../asdf/test_bar.py | 5 +++ .../asdf/test_foo.py | 5 +++ .../test_collect_only_nested_suites/bar.robot | 3 ++ .../test_collect_only_nested_suites/foo.robot | 3 ++ tests/test_python.py | 8 +++++ tests/test_robot.py | 8 +++++ 8 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 tests/fixtures/test_python/test_collect_only_nested_suites/asdf/__init__.py create mode 100644 tests/fixtures/test_python/test_collect_only_nested_suites/asdf/test_bar.py create mode 100644 tests/fixtures/test_python/test_collect_only_nested_suites/asdf/test_foo.py create mode 100644 tests/fixtures/test_robot/test_collect_only_nested_suites/bar.robot create mode 100644 tests/fixtures/test_robot/test_collect_only_nested_suites/foo.robot diff --git a/pytest_robotframework/_common.py b/pytest_robotframework/_common.py index a4648505..ce7a6089 100644 --- a/pytest_robotframework/_common.py +++ b/pytest_robotframework/_common.py @@ -105,22 +105,21 @@ def visit_suite(self, suite: running.TestSuite): item.stash[running_test_case_key] = test_case if self.collect_only: suite.tests.clear() # type:ignore[no-untyped-call] - return - - # remove any .robot tests that were filtered out by pytest (and the fake test - # from `PythonParser`): - for test in suite.tests[:]: - if not get_item_from_robot_test(self.session, test): - suite.tests.remove(test) - - # add any .py tests that were collected by pytest - for item in self.session.items: - if isinstance(item, Function): - module = cast(ModuleType, item.module) - if module.__doc__ and not suite.doc: - suite.doc = module.__doc__ - if item.path == suite.source: - suite.tests.append(item.stash[running_test_case_key]) + else: + # remove any .robot tests that were filtered out by pytest (and the fake test + # from `PythonParser`): + for test in suite.tests[:]: + if not get_item_from_robot_test(self.session, test): + suite.tests.remove(test) + + # add any .py tests that were collected by pytest + for item in self.session.items: + if isinstance(item, Function): + module = cast(ModuleType, item.module) + if module.__doc__ and not suite.doc: + suite.doc = module.__doc__ + if item.path == suite.source: + suite.tests.append(item.stash[running_test_case_key]) super().visit_suite(suite) @override diff --git a/tests/fixtures/test_python/test_collect_only_nested_suites/asdf/__init__.py b/tests/fixtures/test_python/test_collect_only_nested_suites/asdf/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/fixtures/test_python/test_collect_only_nested_suites/asdf/test_bar.py b/tests/fixtures/test_python/test_collect_only_nested_suites/asdf/test_bar.py new file mode 100644 index 00000000..17e3295d --- /dev/null +++ b/tests/fixtures/test_python/test_collect_only_nested_suites/asdf/test_bar.py @@ -0,0 +1,5 @@ +from __future__ import annotations + + +def test_func2(): + pass diff --git a/tests/fixtures/test_python/test_collect_only_nested_suites/asdf/test_foo.py b/tests/fixtures/test_python/test_collect_only_nested_suites/asdf/test_foo.py new file mode 100644 index 00000000..f50e4f57 --- /dev/null +++ b/tests/fixtures/test_python/test_collect_only_nested_suites/asdf/test_foo.py @@ -0,0 +1,5 @@ +from __future__ import annotations + + +def test_func1(): + pass diff --git a/tests/fixtures/test_robot/test_collect_only_nested_suites/bar.robot b/tests/fixtures/test_robot/test_collect_only_nested_suites/bar.robot new file mode 100644 index 00000000..ac14139a --- /dev/null +++ b/tests/fixtures/test_robot/test_collect_only_nested_suites/bar.robot @@ -0,0 +1,3 @@ +*** Test Cases *** +Bar + Fail diff --git a/tests/fixtures/test_robot/test_collect_only_nested_suites/foo.robot b/tests/fixtures/test_robot/test_collect_only_nested_suites/foo.robot new file mode 100644 index 00000000..4a07bdcb --- /dev/null +++ b/tests/fixtures/test_robot/test_collect_only_nested_suites/foo.robot @@ -0,0 +1,3 @@ +*** Test Cases *** +Foo + Fail diff --git a/tests/test_python.py b/tests/test_python.py index a8e67468..640e41c1 100644 --- a/tests/test_python.py +++ b/tests/test_python.py @@ -123,6 +123,14 @@ def test_doesnt_run_when_collecting(pytester_dir: PytesterDir): assert not (pytester_dir.path / "log.html").exists() +# TODO: this test doesnt actually test anything +# https://github.com/DetachHead/pytest-robotframework/issues/61 +def test_collect_only_nested_suites(pytester_dir: PytesterDir): + result = run_pytest(pytester_dir, "--collect-only") + assert result.parseoutcomes() == {"tests": 2} + assert "" in (line.strip() for line in result.outlines) + + def test_correct_items_collected_when_collect_only(pytester_dir: PytesterDir): result = run_pytest(pytester_dir, "--collect-only", "test_bar.py") assert result.parseoutcomes() == {"test": 1} diff --git a/tests/test_robot.py b/tests/test_robot.py index 8ee3cb71..277761dc 100644 --- a/tests/test_robot.py +++ b/tests/test_robot.py @@ -196,6 +196,14 @@ def test_correct_items_collected_when_collect_only(pytester_dir: PytesterDir): assert "" in (line.strip() for line in result.outlines) +# TODO: this test doesnt actually test anything +# https://github.com/DetachHead/pytest-robotframework/issues/61 +def test_collect_only_nested_suites(pytester_dir: PytesterDir): + result = run_pytest(pytester_dir, "--collect-only") + assert result.parseoutcomes() == {"tests": 2} + assert "" in (line.strip() for line in result.outlines) + + def test_doesnt_run_tests_outside_path(pytester_dir: PytesterDir): run_and_assert_result(pytester_dir, pytest_args=["foo"], passed=1) assert_log_file_exists(pytester_dir)