2019-06-05 17:21:44 +08:00
|
|
|
import sys
|
|
|
|
|
2019-04-19 07:23:08 +08:00
|
|
|
import pytest
|
2020-02-04 09:59:20 +08:00
|
|
|
from _pytest.pytester import Testdir
|
2019-04-19 07:23:08 +08:00
|
|
|
|
2019-06-05 17:21:44 +08:00
|
|
|
if sys.gettrace():
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def restore_tracing():
|
|
|
|
"""Restore tracing function (when run with Coverage.py).
|
|
|
|
|
|
|
|
https://bugs.python.org/issue37011
|
|
|
|
"""
|
|
|
|
orig_trace = sys.gettrace()
|
|
|
|
yield
|
|
|
|
if sys.gettrace() != orig_trace:
|
|
|
|
sys.settrace(orig_trace)
|
|
|
|
|
2019-04-19 07:23:08 +08:00
|
|
|
|
|
|
|
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
|
2020-01-17 02:42:29 +08:00
|
|
|
def pytest_collection_modifyitems(items):
|
2019-04-19 07:23:08 +08:00
|
|
|
"""Prefer faster tests.
|
|
|
|
|
|
|
|
Use a hookwrapper to do this in the beginning, so e.g. --ff still works
|
|
|
|
correctly.
|
|
|
|
"""
|
2019-04-08 01:08:59 +08:00
|
|
|
fast_items = []
|
|
|
|
slow_items = []
|
2019-05-27 09:20:58 +08:00
|
|
|
slowest_items = []
|
2019-04-08 01:08:59 +08:00
|
|
|
neutral_items = []
|
|
|
|
|
2019-05-27 09:20:58 +08:00
|
|
|
spawn_names = {"spawn_pytest", "spawn"}
|
2019-04-08 01:08:59 +08:00
|
|
|
|
|
|
|
for item in items:
|
|
|
|
try:
|
|
|
|
fixtures = item.fixturenames
|
|
|
|
except AttributeError:
|
|
|
|
# doctest at least
|
|
|
|
# (https://github.com/pytest-dev/pytest/issues/5070)
|
|
|
|
neutral_items.append(item)
|
|
|
|
else:
|
2019-05-27 09:20:58 +08:00
|
|
|
if "testdir" in fixtures:
|
2019-10-27 09:46:40 +08:00
|
|
|
co_names = item.function.__code__.co_names
|
|
|
|
if spawn_names.intersection(co_names):
|
2019-05-27 09:20:58 +08:00
|
|
|
item.add_marker(pytest.mark.uses_pexpect)
|
2019-05-30 03:30:45 +08:00
|
|
|
slowest_items.append(item)
|
2019-10-27 09:46:40 +08:00
|
|
|
elif "runpytest_subprocess" in co_names:
|
|
|
|
slowest_items.append(item)
|
2019-05-27 09:20:58 +08:00
|
|
|
else:
|
2019-05-30 03:30:45 +08:00
|
|
|
slow_items.append(item)
|
2019-05-30 12:55:38 +08:00
|
|
|
item.add_marker(pytest.mark.slow)
|
2019-04-08 01:08:59 +08:00
|
|
|
else:
|
|
|
|
marker = item.get_closest_marker("slow")
|
|
|
|
if marker:
|
2019-05-27 09:20:58 +08:00
|
|
|
slowest_items.append(item)
|
2019-04-08 01:08:59 +08:00
|
|
|
else:
|
|
|
|
fast_items.append(item)
|
|
|
|
|
2019-05-30 03:30:45 +08:00
|
|
|
items[:] = fast_items + neutral_items + slow_items + slowest_items
|
2019-04-19 07:23:08 +08:00
|
|
|
|
|
|
|
yield
|
2019-08-26 22:32:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def tw_mock():
|
|
|
|
"""Returns a mock terminal writer"""
|
|
|
|
|
|
|
|
class TWMock:
|
|
|
|
WRITE = object()
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.lines = []
|
|
|
|
self.is_writing = False
|
|
|
|
|
|
|
|
def sep(self, sep, line=None):
|
|
|
|
self.lines.append((sep, line))
|
|
|
|
|
|
|
|
def write(self, msg, **kw):
|
|
|
|
self.lines.append((TWMock.WRITE, msg))
|
|
|
|
|
|
|
|
def line(self, line, **kw):
|
|
|
|
self.lines.append(line)
|
|
|
|
|
|
|
|
def markup(self, text, **kw):
|
|
|
|
return text
|
|
|
|
|
|
|
|
def get_write_msg(self, idx):
|
|
|
|
flag, msg = self.lines[idx]
|
|
|
|
assert flag == TWMock.WRITE
|
|
|
|
return msg
|
|
|
|
|
|
|
|
fullwidth = 80
|
|
|
|
|
|
|
|
return TWMock()
|
2019-09-28 22:52:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def dummy_yaml_custom_test(testdir):
|
|
|
|
"""Writes a conftest file that collects and executes a dummy yaml test.
|
|
|
|
|
|
|
|
Taken from the docs, but stripped down to the bare minimum, useful for
|
|
|
|
tests which needs custom items collected.
|
|
|
|
"""
|
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def pytest_collect_file(parent, path):
|
|
|
|
if path.ext == ".yaml" and path.basename.startswith("test"):
|
|
|
|
return YamlFile(path, parent)
|
|
|
|
|
|
|
|
class YamlFile(pytest.File):
|
|
|
|
def collect(self):
|
|
|
|
yield YamlItem(self.fspath.basename, self)
|
|
|
|
|
|
|
|
class YamlItem(pytest.Item):
|
|
|
|
def runtest(self):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makefile(".yaml", test1="")
|
2020-02-04 09:59:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def testdir(testdir: Testdir) -> Testdir:
|
|
|
|
testdir.monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1")
|
|
|
|
return testdir
|