Skip to content

Commit

Permalink
fix collecting on nested suites not removing the fake test
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Aug 30, 2023
1 parent a207c26 commit d124b2a
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 16 deletions.
31 changes: 15 additions & 16 deletions pytest_robotframework/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from __future__ import annotations


def test_func2():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from __future__ import annotations


def test_func1():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*** Test Cases ***
Bar
Fail
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*** Test Cases ***
Foo
Fail
8 changes: 8 additions & 0 deletions tests/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<Function test_func2>" 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}
Expand Down
8 changes: 8 additions & 0 deletions tests/test_robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ def test_correct_items_collected_when_collect_only(pytester_dir: PytesterDir):
assert "<RobotItem Bar>" 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 "<RobotItem Bar>" 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)
Expand Down

0 comments on commit d124b2a

Please sign in to comment.