Skip to content

Commit

Permalink
tests: Migrate to pytester - incremental update (#8145)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonblr committed Dec 15, 2020
1 parent cb8142b commit 8eef8c6
Show file tree
Hide file tree
Showing 11 changed files with 801 additions and 703 deletions.
57 changes: 33 additions & 24 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import queue
import sys
import textwrap
from pathlib import Path
from typing import Any
from typing import Dict
from typing import Tuple
Expand All @@ -19,7 +20,10 @@
from _pytest._code.code import ExceptionInfo
from _pytest._code.code import FormattedExcinfo
from _pytest._io import TerminalWriter
from _pytest.pathlib import import_path
from _pytest.pytester import LineMatcher
from _pytest.pytester import Pytester


if TYPE_CHECKING:
from _pytest._code.code import _TracebackStyle
Expand Down Expand Up @@ -155,10 +159,10 @@ def test_traceback_cut(self):
newtraceback = traceback.cut(path=path, lineno=firstlineno + 2)
assert len(newtraceback) == 1

def test_traceback_cut_excludepath(self, testdir):
p = testdir.makepyfile("def f(): raise ValueError")
def test_traceback_cut_excludepath(self, pytester: Pytester) -> None:
p = pytester.makepyfile("def f(): raise ValueError")
with pytest.raises(ValueError) as excinfo:
p.pyimport().f()
import_path(p).f() # type: ignore[attr-defined]
basedir = py.path.local(pytest.__file__).dirpath()
newtraceback = excinfo.traceback.cut(excludepath=basedir)
for x in newtraceback:
Expand Down Expand Up @@ -406,8 +410,8 @@ def test_match_succeeds():
excinfo.match(r".*zero.*")


def test_match_raises_error(testdir):
testdir.makepyfile(
def test_match_raises_error(pytester: Pytester) -> None:
pytester.makepyfile(
"""
import pytest
def test_division_zero():
Expand All @@ -416,14 +420,14 @@ def test_division_zero():
excinfo.match(r'[123]+')
"""
)
result = testdir.runpytest()
result = pytester.runpytest()
assert result.ret != 0

exc_msg = "Regex pattern '[[]123[]]+' does not match 'division by zero'."
result.stdout.fnmatch_lines([f"E * AssertionError: {exc_msg}"])
result.stdout.no_fnmatch_line("*__tracebackhide__ = True*")

result = testdir.runpytest("--fulltrace")
result = pytester.runpytest("--fulltrace")
assert result.ret != 0
result.stdout.fnmatch_lines(
["*__tracebackhide__ = True*", f"E * AssertionError: {exc_msg}"]
Expand All @@ -432,15 +436,14 @@ def test_division_zero():

class TestFormattedExcinfo:
@pytest.fixture
def importasmod(self, request, _sys_snapshot):
def importasmod(self, tmp_path: Path, _sys_snapshot):
def importasmod(source):
source = textwrap.dedent(source)
tmpdir = request.getfixturevalue("tmpdir")
modpath = tmpdir.join("mod.py")
tmpdir.ensure("__init__.py")
modpath.write(source)
modpath = tmp_path.joinpath("mod.py")
tmp_path.joinpath("__init__.py").touch()
modpath.write_text(source)
importlib.invalidate_caches()
return modpath.pyimport()
return import_path(modpath)

return importasmod

Expand Down Expand Up @@ -682,7 +685,7 @@ def entry():
p = FormattedExcinfo(style="short")
reprtb = p.repr_traceback_entry(excinfo.traceback[-2])
lines = reprtb.lines
basename = py.path.local(mod.__file__).basename
basename = Path(mod.__file__).name
assert lines[0] == " func1()"
assert reprtb.reprfileloc is not None
assert basename in str(reprtb.reprfileloc.path)
Expand Down Expand Up @@ -948,7 +951,9 @@ def f():
assert line.endswith("mod.py")
assert tw_mock.lines[12] == ":3: ValueError"

def test_toterminal_long_missing_source(self, importasmod, tmpdir, tw_mock):
def test_toterminal_long_missing_source(
self, importasmod, tmp_path: Path, tw_mock
) -> None:
mod = importasmod(
"""
def g(x):
Expand All @@ -958,7 +963,7 @@ def f():
"""
)
excinfo = pytest.raises(ValueError, mod.f)
tmpdir.join("mod.py").remove()
tmp_path.joinpath("mod.py").unlink()
excinfo.traceback = excinfo.traceback.filter()
repr = excinfo.getrepr()
repr.toterminal(tw_mock)
Expand All @@ -978,7 +983,9 @@ def f():
assert line.endswith("mod.py")
assert tw_mock.lines[10] == ":3: ValueError"

def test_toterminal_long_incomplete_source(self, importasmod, tmpdir, tw_mock):
def test_toterminal_long_incomplete_source(
self, importasmod, tmp_path: Path, tw_mock
) -> None:
mod = importasmod(
"""
def g(x):
Expand All @@ -988,7 +995,7 @@ def f():
"""
)
excinfo = pytest.raises(ValueError, mod.f)
tmpdir.join("mod.py").write("asdf")
tmp_path.joinpath("mod.py").write_text("asdf")
excinfo.traceback = excinfo.traceback.filter()
repr = excinfo.getrepr()
repr.toterminal(tw_mock)
Expand Down Expand Up @@ -1374,16 +1381,18 @@ def test_repr_traceback_with_unicode(style, encoding):
assert repr_traceback is not None


def test_cwd_deleted(testdir):
testdir.makepyfile(
def test_cwd_deleted(pytester: Pytester) -> None:
pytester.makepyfile(
"""
def test(tmpdir):
tmpdir.chdir()
tmpdir.remove()
import os
def test(tmp_path):
os.chdir(tmp_path)
tmp_path.unlink()
assert False
"""
)
result = testdir.runpytest()
result = pytester.runpytest()
result.stdout.fnmatch_lines(["* 1 failed in *"])
result.stdout.no_fnmatch_line("*INTERNALERROR*")
result.stderr.no_fnmatch_line("*INTERNALERROR*")
Expand Down
9 changes: 6 additions & 3 deletions testing/examples/test_issue519.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
def test_510(testdir):
testdir.copy_example("issue_519.py")
testdir.runpytest("issue_519.py")
from _pytest.pytester import Pytester


def test_510(pytester: Pytester) -> None:
pytester.copy_example("issue_519.py")
pytester.runpytest("issue_519.py")
5 changes: 3 additions & 2 deletions testing/io/test_terminalwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import shutil
import sys
from pathlib import Path
from typing import Generator
from unittest import mock

Expand Down Expand Up @@ -64,10 +65,10 @@ def test_terminalwriter_not_unicode() -> None:
class TestTerminalWriter:
@pytest.fixture(params=["path", "stringio"])
def tw(
self, request, tmpdir
self, request, tmp_path: Path
) -> Generator[terminalwriter.TerminalWriter, None, None]:
if request.param == "path":
p = tmpdir.join("tmpfile")
p = tmp_path.joinpath("tmpfile")
f = open(str(p), "w+", encoding="utf8")
tw = terminalwriter.TerminalWriter(f)

Expand Down
50 changes: 26 additions & 24 deletions testing/logging/test_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import pytest
from _pytest.logging import caplog_records_key
from _pytest.pytester import Testdir
from _pytest.pytester import Pytester

logger = logging.getLogger(__name__)
sublogger = logging.getLogger(__name__ + ".baz")


def test_fixture_help(testdir):
result = testdir.runpytest("--fixtures")
def test_fixture_help(pytester: Pytester) -> None:
result = pytester.runpytest("--fixtures")
result.stdout.fnmatch_lines(["*caplog*"])


Expand All @@ -28,12 +28,12 @@ def test_change_level(caplog):
assert "CRITICAL" in caplog.text


def test_change_level_undo(testdir: Testdir) -> None:
def test_change_level_undo(pytester: Pytester) -> None:
"""Ensure that 'set_level' is undone after the end of the test.
Tests the logging output themselves (affacted both by logger and handler levels).
"""
testdir.makepyfile(
pytester.makepyfile(
"""
import logging
Expand All @@ -49,17 +49,17 @@ def test2(caplog):
assert 0
"""
)
result = testdir.runpytest()
result = pytester.runpytest()
result.stdout.fnmatch_lines(["*log from test1*", "*2 failed in *"])
result.stdout.no_fnmatch_line("*log from test2*")


def test_change_level_undos_handler_level(testdir: Testdir) -> None:
def test_change_level_undos_handler_level(pytester: Pytester) -> None:
"""Ensure that 'set_level' is undone after the end of the test (handler).
Issue #7569. Tests the handler level specifically.
"""
testdir.makepyfile(
pytester.makepyfile(
"""
import logging
Expand All @@ -78,7 +78,7 @@ def test3(caplog):
assert caplog.handler.level == 43
"""
)
result = testdir.runpytest()
result = pytester.runpytest()
result.assert_outcomes(passed=3)


Expand Down Expand Up @@ -172,8 +172,8 @@ def test_caplog_captures_for_all_stages(caplog, logging_during_setup_and_teardow
assert set(caplog._item._store[caplog_records_key]) == {"setup", "call"}


def test_ini_controls_global_log_level(testdir):
testdir.makepyfile(
def test_ini_controls_global_log_level(pytester: Pytester) -> None:
pytester.makepyfile(
"""
import pytest
import logging
Expand All @@ -187,20 +187,20 @@ def test_log_level_override(request, caplog):
assert 'ERROR' in caplog.text
"""
)
testdir.makeini(
pytester.makeini(
"""
[pytest]
log_level=ERROR
"""
)

result = testdir.runpytest()
result = pytester.runpytest()
# make sure that that we get a '0' exit code for the testsuite
assert result.ret == 0


def test_caplog_can_override_global_log_level(testdir):
testdir.makepyfile(
def test_caplog_can_override_global_log_level(pytester: Pytester) -> None:
pytester.makepyfile(
"""
import pytest
import logging
Expand All @@ -227,19 +227,19 @@ def test_log_level_override(request, caplog):
assert "message won't be shown" not in caplog.text
"""
)
testdir.makeini(
pytester.makeini(
"""
[pytest]
log_level=WARNING
"""
)

result = testdir.runpytest()
result = pytester.runpytest()
assert result.ret == 0


def test_caplog_captures_despite_exception(testdir):
testdir.makepyfile(
def test_caplog_captures_despite_exception(pytester: Pytester) -> None:
pytester.makepyfile(
"""
import pytest
import logging
Expand All @@ -255,26 +255,28 @@ def test_log_level_override(request, caplog):
raise Exception()
"""
)
testdir.makeini(
pytester.makeini(
"""
[pytest]
log_level=WARNING
"""
)

result = testdir.runpytest()
result = pytester.runpytest()
result.stdout.fnmatch_lines(["*ERROR message will be shown*"])
result.stdout.no_fnmatch_line("*DEBUG message won't be shown*")
assert result.ret == 1


def test_log_report_captures_according_to_config_option_upon_failure(testdir):
def test_log_report_captures_according_to_config_option_upon_failure(
pytester: Pytester,
) -> None:
"""Test that upon failure:
(1) `caplog` succeeded to capture the DEBUG message and assert on it => No `Exception` is raised.
(2) The `DEBUG` message does NOT appear in the `Captured log call` report.
(3) The stdout, `INFO`, and `WARNING` messages DO appear in the test reports due to `--log-level=INFO`.
"""
testdir.makepyfile(
pytester.makepyfile(
"""
import pytest
import logging
Expand All @@ -299,7 +301,7 @@ def test_that_fails(request, caplog):
"""
)

result = testdir.runpytest("--log-level=INFO")
result = pytester.runpytest("--log-level=INFO")
result.stdout.no_fnmatch_line("*Exception: caplog failed to capture DEBUG*")
result.stdout.no_fnmatch_line("*DEBUG log message*")
result.stdout.fnmatch_lines(
Expand Down

0 comments on commit 8eef8c6

Please sign in to comment.