Merge pull request #8019 from JosiasAurel/mypytester-change-01

Migrate from testdir to pytester
This commit is contained in:
Ran Benita 2020-11-13 10:36:03 +02:00 committed by GitHub
commit ea3c0aa245
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 18 deletions

View File

@ -1,26 +1,27 @@
import sys
import pytest
from _pytest.pytester import Pytester
def test_enabled(testdir):
def test_enabled(pytester: Pytester) -> None:
"""Test single crashing test displays a traceback."""
testdir.makepyfile(
pytester.makepyfile(
"""
import faulthandler
def test_crash():
faulthandler._sigabrt()
"""
)
result = testdir.runpytest_subprocess()
result = pytester.runpytest_subprocess()
result.stderr.fnmatch_lines(["*Fatal Python error*"])
assert result.ret != 0
def test_crash_near_exit(testdir):
def test_crash_near_exit(pytester: Pytester) -> None:
"""Test that fault handler displays crashes that happen even after
pytest is exiting (for example, when the interpreter is shutting down)."""
testdir.makepyfile(
pytester.makepyfile(
"""
import faulthandler
import atexit
@ -28,21 +29,21 @@ def test_crash_near_exit(testdir):
atexit.register(faulthandler._sigabrt)
"""
)
result = testdir.runpytest_subprocess()
result = pytester.runpytest_subprocess()
result.stderr.fnmatch_lines(["*Fatal Python error*"])
assert result.ret != 0
def test_disabled(testdir):
def test_disabled(pytester: Pytester) -> None:
"""Test option to disable fault handler in the command line."""
testdir.makepyfile(
pytester.makepyfile(
"""
import faulthandler
def test_disabled():
assert not faulthandler.is_enabled()
"""
)
result = testdir.runpytest_subprocess("-p", "no:faulthandler")
result = pytester.runpytest_subprocess("-p", "no:faulthandler")
result.stdout.fnmatch_lines(["*1 passed*"])
assert result.ret == 0
@ -56,19 +57,19 @@ def test_disabled(testdir):
False,
],
)
def test_timeout(testdir, enabled: bool) -> None:
def test_timeout(pytester: Pytester, enabled: bool) -> None:
"""Test option to dump tracebacks after a certain timeout.
If faulthandler is disabled, no traceback will be dumped.
"""
testdir.makepyfile(
pytester.makepyfile(
"""
import os, time
def test_timeout():
time.sleep(1 if "CI" in os.environ else 0.1)
"""
)
testdir.makeini(
pytester.makeini(
"""
[pytest]
faulthandler_timeout = 0.01
@ -76,7 +77,7 @@ def test_timeout(testdir, enabled: bool) -> None:
)
args = ["-p", "no:faulthandler"] if not enabled else []
result = testdir.runpytest_subprocess(*args)
result = pytester.runpytest_subprocess(*args)
tb_output = "most recent call first"
if enabled:
result.stderr.fnmatch_lines(["*%s*" % tb_output])
@ -87,7 +88,7 @@ def test_timeout(testdir, enabled: bool) -> None:
@pytest.mark.parametrize("hook_name", ["pytest_enter_pdb", "pytest_exception_interact"])
def test_cancel_timeout_on_hook(monkeypatch, hook_name):
def test_cancel_timeout_on_hook(monkeypatch, hook_name) -> None:
"""Make sure that we are cancelling any scheduled traceback dumping due
to timeout before entering pdb (pytest-dev/pytest-faulthandler#12) or any
other interactive exception (pytest-dev/pytest-faulthandler#14)."""
@ -108,21 +109,21 @@ def test_cancel_timeout_on_hook(monkeypatch, hook_name):
@pytest.mark.parametrize("faulthandler_timeout", [0, 2])
def test_already_initialized(faulthandler_timeout, testdir):
def test_already_initialized(faulthandler_timeout: int, pytester: Pytester) -> None:
"""Test for faulthandler being initialized earlier than pytest (#6575)."""
testdir.makepyfile(
pytester.makepyfile(
"""
def test():
import faulthandler
assert faulthandler.is_enabled()
"""
)
result = testdir.run(
result = pytester.run(
sys.executable,
"-X",
"faulthandler",
"-mpytest",
testdir.tmpdir,
pytester.path,
"-o",
f"faulthandler_timeout={faulthandler_timeout}",
)