Migrate from testdir to pytester

This commit is contained in:
Josias Aurel 2020-11-09 18:07:34 +01:00 committed by GitHub
parent b2e7b9df9e
commit 043ed55056
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 18 deletions

View File

@ -1,26 +1,26 @@
import sys import sys
import pytest import pytest
from _pytest.pytester import Pytester
def test_enabled(pytester: Pytester) -> None:
def test_enabled(testdir):
"""Test single crashing test displays a traceback.""" """Test single crashing test displays a traceback."""
testdir.makepyfile( pytester.makepyfile(
""" """
import faulthandler import faulthandler
def test_crash(): def test_crash():
faulthandler._sigabrt() faulthandler._sigabrt()
""" """
) )
result = testdir.runpytest_subprocess() result = pytester.runpytest_subprocess()
result.stderr.fnmatch_lines(["*Fatal Python error*"]) result.stderr.fnmatch_lines(["*Fatal Python error*"])
assert result.ret != 0 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 """Test that fault handler displays crashes that happen even after
pytest is exiting (for example, when the interpreter is shutting down).""" pytest is exiting (for example, when the interpreter is shutting down)."""
testdir.makepyfile( pytester.makepyfile(
""" """
import faulthandler import faulthandler
import atexit import atexit
@ -28,21 +28,21 @@ def test_crash_near_exit(testdir):
atexit.register(faulthandler._sigabrt) atexit.register(faulthandler._sigabrt)
""" """
) )
result = testdir.runpytest_subprocess() result = pytester.runpytest_subprocess()
result.stderr.fnmatch_lines(["*Fatal Python error*"]) result.stderr.fnmatch_lines(["*Fatal Python error*"])
assert result.ret != 0 assert result.ret != 0
def test_disabled(testdir): def test_disabled(pytester: Pytester) -> None:
"""Test option to disable fault handler in the command line.""" """Test option to disable fault handler in the command line."""
testdir.makepyfile( pytester.makepyfile(
""" """
import faulthandler import faulthandler
def test_disabled(): def test_disabled():
assert not faulthandler.is_enabled() 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*"]) result.stdout.fnmatch_lines(["*1 passed*"])
assert result.ret == 0 assert result.ret == 0
@ -56,19 +56,19 @@ def test_disabled(testdir):
False, 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. """Test option to dump tracebacks after a certain timeout.
If faulthandler is disabled, no traceback will be dumped. If faulthandler is disabled, no traceback will be dumped.
""" """
testdir.makepyfile( pytester.makepyfile(
""" """
import os, time import os, time
def test_timeout(): def test_timeout():
time.sleep(1 if "CI" in os.environ else 0.1) time.sleep(1 if "CI" in os.environ else 0.1)
""" """
) )
testdir.makeini( pytester.makeini(
""" """
[pytest] [pytest]
faulthandler_timeout = 0.01 faulthandler_timeout = 0.01
@ -76,7 +76,7 @@ def test_timeout(testdir, enabled: bool) -> None:
) )
args = ["-p", "no:faulthandler"] if not enabled else [] args = ["-p", "no:faulthandler"] if not enabled else []
result = testdir.runpytest_subprocess(*args) result = pytester.runpytest_subprocess(*args)
tb_output = "most recent call first" tb_output = "most recent call first"
if enabled: if enabled:
result.stderr.fnmatch_lines(["*%s*" % tb_output]) result.stderr.fnmatch_lines(["*%s*" % tb_output])
@ -108,21 +108,21 @@ def test_cancel_timeout_on_hook(monkeypatch, hook_name):
@pytest.mark.parametrize("faulthandler_timeout", [0, 2]) @pytest.mark.parametrize("faulthandler_timeout", [0, 2])
def test_already_initialized(faulthandler_timeout, testdir): def test_already_initialized(faulthandler_timeout, pytester: Pytester):
"""Test for faulthandler being initialized earlier than pytest (#6575).""" """Test for faulthandler being initialized earlier than pytest (#6575)."""
testdir.makepyfile( pytester.makepyfile(
""" """
def test(): def test():
import faulthandler import faulthandler
assert faulthandler.is_enabled() assert faulthandler.is_enabled()
""" """
) )
result = testdir.run( result = pytester.run(
sys.executable, sys.executable,
"-X", "-X",
"faulthandler", "faulthandler",
"-mpytest", "-mpytest",
testdir.tmpdir, pytester.tmpdir,
"-o", "-o",
f"faulthandler_timeout={faulthandler_timeout}", f"faulthandler_timeout={faulthandler_timeout}",
) )