Skip to content

Commit

Permalink
Message when test collection fails is clearer (#11)
Browse files Browse the repository at this point in the history
Co-authored-by: Maciej Gol <[email protected]>
  • Loading branch information
maciej-gol and Maciej Gol authored Jun 9, 2024
1 parent 75ec599 commit d90adc5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions integration_data/collection_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
raise Exception("This is a test collection error")

def test_ok():
...
13 changes: 12 additions & 1 deletion pytest_bisect_tests/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import argparse
import sys
from pytest_bisect_tests.bisect import run_bisect

from pytest_bisect_tests.pytest_runner import (
PytestRunner,
TestCollectionError,
)


Expand Down Expand Up @@ -34,8 +36,17 @@ def main() -> None:
collect_options=args.collect_options,
stdout=args.stdout,
)
try:
test_names = pytest_runner.collect_tests()
except TestCollectionError:
if not args.stdout:
print("Failed to collect tests. Use --stdout to see the output from pytest test collection.")
else:
print("Failed to collect tests. Consult output from pytest.")
sys.exit(1)

result = run_bisect(
test_names=pytest_runner.collect_tests(),
test_names=test_names,
failing_test=args.failing_test,
test_runner=pytest_runner.run,
)
Expand Down
5 changes: 4 additions & 1 deletion pytest_bisect_tests/pytest_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import subprocess
from typing import List, Optional

class TestCollectionError(Exception):
pass


class PytestRunner:
def __init__(
Expand Down Expand Up @@ -42,7 +45,7 @@ def collect_tests(self) -> List[str]:
with os.fdopen(r, closefd=False) as f:
tests = [l.strip() for l in f.readlines()]
if process.wait() != 0:
raise RuntimeError("Failed to collect tests.")
raise TestCollectionError("Failed to collect tests.")
return tests
finally:
os.closerange(r, w)
Expand Down
27 changes: 26 additions & 1 deletion tests/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@


def _standalone_caller(
failing_test: str, collect_options: str, run_options: str
failing_test: str, collect_options: str, run_options: str, stdout: bool = False
) -> Tuple[str, int]:
args = ["pytest-bisect-tests", "--failing-test", failing_test]
if collect_options:
args.extend(["--collect-options", collect_options])
if stdout:
args.append("--stdout")
if run_options:
args.extend(["--run-options", run_options])

Expand Down Expand Up @@ -91,3 +93,26 @@ def test_should_work_with_items_modifying_plugins(plugin_caller: PluginCaller) -

assert "Faulty test: integration_data/test_groups_test.py::test_faulty" in out
assert code == 0

def test_cli_should_communicate_stdout_argument() -> None:
out, code = _standalone_caller(
failing_test="integration_data/collection_error.py::test_non_existing_fixture",
collect_options="",
run_options="collection_error.py",
)

assert code == 1
assert "Failed to collect tests. Use --stdout to see the output from pytest test collection." in out


def test_cli_should_show_output_from_collection_with_stdout() -> None:
out, code = _standalone_caller(
failing_test="integration_data/collection_error.py::test_non_existing_fixture",
collect_options="",
run_options="collection_error.py",
stdout=True,
)

assert code == 1
assert "Exception: This is a test collection error" in out
assert "Failed to collect tests. Consult output from pytest." in out

0 comments on commit d90adc5

Please sign in to comment.