Skip to content

Commit

Permalink
Disable pytest-pystack for test depending on pytester fixture
Browse files Browse the repository at this point in the history
This is only a workaround, maybe a better solution will be to implement
some stack mechanism, that will be enabled if pytester fixture is used.

Signed-off-by: Grzegorz Bokota <[email protected]>
  • Loading branch information
Czaki authored and pablogsal committed Nov 16, 2024
1 parent 77e9a06 commit 54ab688
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ format:

.PHONY: lint
lint:
$(PYTHON) -m ruff $(python_files)
$(PYTHON) -m ruff check $(python_files)
$(PYTHON) -m isort --check $(python_files)
$(PYTHON) -m black --check $(python_files)
$(PYTHON) -m black --check $(python_files)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

A pytest plug-in for easy integration of PyStack in your test suite.

It can be used to automatically dump the stack trace of a hanging test in your suite.
It can be used to automatically dump the stack trace of a hanging test in your suite (with exception to test using `pytester` fixture).

See [PyStack](https://github.com/bloomberg/pystack) for further information about the tool.

Expand Down
7 changes: 6 additions & 1 deletion src/pytest_pystack/_monitor_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ def _run_monitor(config: PystackConfig, pid, queue):
handled_test_cases.add(testcase)

try:
if queue.get(timeout=config.threshold) != testcase:
new_testcase = queue.get(timeout=config.threshold)
if new_testcase != testcase:
print(
f"new test {new_testcase} should not start before previous {testcase} test finished",
file=sys.__stderr__,
)
raise Exception(
"new test should not start before previous test finished"
)
Expand Down
6 changes: 5 additions & 1 deletion src/pytest_pystack/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ def pytest_addoption(parser) -> None:

@pytest.hookimpl
def pytest_runtest_makereport(item, call):
if call.when in {"setup", "teardown"} and item.config._pystack_queue:
if (
call.when in {"setup", "teardown"}
and item.config._pystack_queue
and "pytester" not in item.fixturenames
):
item.config._pystack_queue.put(item.name)


Expand Down
35 changes: 35 additions & 0 deletions tests/test_pytest_pystack.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,38 @@ def test_two_slow_tests_in_a_suite_prints_both(testdir, monkeypatch, capfd):
print(stderr)
assert "PYSTACK -- test_sleeping_test" in stderr
assert "PYSTACK -- test_sleeping_test2" in stderr


def test_pytester_compat(testdir, capfd, monkeypatch):
"""Make sure that our make_napari_viewer plugin works."""

# create a temporary pytest test file

monkeypatch.chdir(testdir.tmpdir)
testdir.makepyfile(
"""
pytest_plugins = 'pytester'
test_file ='''
import time
def test_sleep():
time.sleep(1)
assert 1 == 1
'''
def test_pytester(pytester):
pytester.makepyfile(test_file)
result = pytester.runpytest()
result.assert_outcomes(passed=1)
"""
)
result = testdir.runpytest("--pystack-threshold=3", "-s")

# check that all 1 test passed
result.assert_outcomes(passed=1)

_, stderr = capfd.readouterr()
assert not stderr

0 comments on commit 54ab688

Please sign in to comment.