Skip to content

Commit 9701c26

Browse files
committed
Add empty test case when no test cases exist to ensure pytest exit code 0
1 parent bd2d313 commit 9701c26

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/pynguin/testcase/export.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,15 @@ def to_module(self) -> tuple[ast.Module, bool]:
290290
)
291291
import_nodes.append(sut_import)
292292

293+
# Create a single empty test case to ensure pytest returns exit code 0
294+
empty_test = PyTestChromosomeToAstVisitor.__create_function_node(
295+
"empty",
296+
[ast.Pass()],
297+
with_self_arg=False,
298+
is_failing=False,
299+
)
300+
functions.append(empty_test)
301+
293302
return ast.Module(body=import_nodes + functions, type_ignores=[]), coverage_by_import_only
294303

295304

tests/testcase/export/test_export.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ def test_import_export_no_xfail(
531531

532532

533533
def test_coverage_by_import_only(tmp_path: Path) -> None:
534-
"""When no test cases exist, SUT is imported with coverage comment and noqa."""
534+
"""When no test cases exist, SUT is imported with coverage comment and an empty test."""
535535
sut_module_name = "tests.fixtures.accessibles.accessible"
536536
visitor = export.PyTestChromosomeToAstVisitor(sut_module_name=sut_module_name)
537537

@@ -552,3 +552,5 @@ def test_coverage_by_import_only(tmp_path: Path) -> None:
552552
assert "# Importing this module achieves coverage." in content
553553
assert "# noqa: F401" in content
554554
assert "import tests.fixtures.accessibles.accessible" in content
555+
assert "def test_empty():" in content
556+
assert " pass" in content

0 commit comments

Comments
 (0)