2013-10-12 21:39:22 +08:00
|
|
|
import os
|
2019-04-06 15:55:56 +08:00
|
|
|
import subprocess
|
2017-12-09 04:04:52 +08:00
|
|
|
import sys
|
2018-10-05 10:11:02 +08:00
|
|
|
import time
|
2018-10-25 15:01:29 +08:00
|
|
|
|
|
|
|
import py.path
|
|
|
|
|
2017-12-09 04:04:52 +08:00
|
|
|
import _pytest.pytester as pytester
|
2018-10-25 15:01:29 +08:00
|
|
|
import pytest
|
2015-04-22 19:31:46 +08:00
|
|
|
from _pytest.config import PytestPluginManager
|
2019-06-07 18:58:51 +08:00
|
|
|
from _pytest.main import ExitCode
|
2018-10-25 15:01:29 +08:00
|
|
|
from _pytest.pytester import CwdSnapshot
|
|
|
|
from _pytest.pytester import HookRecorder
|
2019-03-15 11:14:07 +08:00
|
|
|
from _pytest.pytester import LineMatcher
|
2018-10-25 15:01:29 +08:00
|
|
|
from _pytest.pytester import SysModulesSnapshot
|
|
|
|
from _pytest.pytester import SysPathsSnapshot
|
2015-04-21 10:18:04 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2014-10-06 19:37:57 +08:00
|
|
|
def test_make_hook_recorder(testdir):
|
2009-09-06 22:59:39 +08:00
|
|
|
item = testdir.getitem("def test_func(): pass")
|
2014-10-06 19:37:57 +08:00
|
|
|
recorder = testdir.make_hook_recorder(item.config.pluginmanager)
|
2009-12-29 19:36:17 +08:00
|
|
|
assert not recorder.getfailures()
|
2010-09-15 16:30:50 +08:00
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.xfail("internal reportrecorder tests need refactoring")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class rep:
|
2009-09-06 22:59:39 +08:00
|
|
|
excinfo = None
|
|
|
|
passed = False
|
2010-07-27 03:15:15 +08:00
|
|
|
failed = True
|
2009-09-06 22:59:39 +08:00
|
|
|
skipped = False
|
2010-07-27 03:15:15 +08:00
|
|
|
when = "call"
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
recorder.hook.pytest_runtest_logreport(report=rep)
|
|
|
|
failures = recorder.getfailures()
|
|
|
|
assert failures == [rep]
|
|
|
|
failures = recorder.getfailures()
|
|
|
|
assert failures == [rep]
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class rep:
|
2009-09-06 22:59:39 +08:00
|
|
|
excinfo = None
|
|
|
|
passed = False
|
|
|
|
failed = False
|
|
|
|
skipped = True
|
2010-07-27 03:15:15 +08:00
|
|
|
when = "call"
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
rep.passed = False
|
|
|
|
rep.skipped = True
|
|
|
|
recorder.hook.pytest_runtest_logreport(report=rep)
|
|
|
|
|
|
|
|
modcol = testdir.getmodulecol("")
|
|
|
|
rep = modcol.config.hook.pytest_make_collect_report(collector=modcol)
|
|
|
|
rep.passed = False
|
|
|
|
rep.failed = True
|
|
|
|
rep.skipped = False
|
|
|
|
recorder.hook.pytest_collectreport(report=rep)
|
|
|
|
|
|
|
|
passed, skipped, failed = recorder.listoutcomes()
|
|
|
|
assert not passed and skipped and failed
|
|
|
|
|
|
|
|
numpassed, numskipped, numfailed = recorder.countoutcomes()
|
|
|
|
assert numpassed == 0
|
|
|
|
assert numskipped == 1
|
|
|
|
assert numfailed == 1
|
|
|
|
assert len(recorder.getfailedcollections()) == 1
|
|
|
|
|
|
|
|
recorder.unregister()
|
2010-07-27 03:15:15 +08:00
|
|
|
recorder.clear()
|
2009-09-06 22:59:39 +08:00
|
|
|
recorder.hook.pytest_runtest_logreport(report=rep)
|
2018-11-23 02:05:10 +08:00
|
|
|
pytest.raises(ValueError, recorder.getfailures)
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_parseconfig(testdir):
|
|
|
|
config1 = testdir.parseconfig()
|
|
|
|
config2 = testdir.parseconfig()
|
2019-06-30 23:24:51 +08:00
|
|
|
assert config2 is not config1
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_testdir_runs_with_plugin(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2015-04-22 19:44:37 +08:00
|
|
|
pytest_plugins = "pytester"
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_hello(testdir):
|
|
|
|
assert 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2009-09-06 22:59:39 +08:00
|
|
|
result = testdir.runpytest()
|
2015-04-28 17:54:53 +08:00
|
|
|
result.assert_outcomes(passed=1)
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2014-10-01 18:20:11 +08:00
|
|
|
|
2018-08-20 14:10:44 +08:00
|
|
|
def test_runresult_assertion_on_xfail(testdir):
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
pytest_plugins = "pytester"
|
|
|
|
|
|
|
|
@pytest.mark.xfail
|
|
|
|
def test_potato():
|
|
|
|
assert False
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.assert_outcomes(xfailed=1)
|
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_runresult_assertion_on_xpassed(testdir):
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
pytest_plugins = "pytester"
|
|
|
|
|
|
|
|
@pytest.mark.xfail
|
|
|
|
def test_potato():
|
|
|
|
assert True
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.assert_outcomes(xpassed=1)
|
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
2019-01-19 00:35:49 +08:00
|
|
|
def test_runresult_repr():
|
|
|
|
from _pytest.pytester import RunResult
|
|
|
|
|
|
|
|
assert (
|
|
|
|
repr(
|
|
|
|
RunResult(ret="ret", outlines=[""], errlines=["some", "errors"], duration=1)
|
|
|
|
)
|
|
|
|
== "<RunResult ret='ret' len(stdout.lines)=1 len(stderr.lines)=2 duration=1.00s>"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-08-20 14:10:44 +08:00
|
|
|
def test_xpassed_with_strict_is_considered_a_failure(testdir):
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
pytest_plugins = "pytester"
|
|
|
|
|
|
|
|
@pytest.mark.xfail(strict=True)
|
|
|
|
def test_potato():
|
|
|
|
assert True
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.assert_outcomes(failed=1)
|
|
|
|
assert result.ret != 0
|
|
|
|
|
|
|
|
|
2014-10-01 18:20:11 +08:00
|
|
|
def make_holder():
|
2019-06-03 06:32:00 +08:00
|
|
|
class apiclass:
|
2010-10-12 19:10:39 +08:00
|
|
|
def pytest_xyz(self, arg):
|
|
|
|
"x"
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2014-10-01 18:20:11 +08:00
|
|
|
def pytest_xyz_noarg(self):
|
|
|
|
"x"
|
2010-10-12 19:10:39 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
apimod = type(os)("api")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-10-01 18:20:11 +08:00
|
|
|
def pytest_xyz(arg):
|
|
|
|
"x"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-10-01 18:20:11 +08:00
|
|
|
def pytest_xyz_noarg():
|
2010-10-12 19:10:39 +08:00
|
|
|
"x"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-10-12 19:10:39 +08:00
|
|
|
apimod.pytest_xyz = pytest_xyz
|
2014-10-01 18:20:11 +08:00
|
|
|
apimod.pytest_xyz_noarg = pytest_xyz_noarg
|
|
|
|
return apiclass, apimod
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("holder", make_holder())
|
|
|
|
def test_hookrecorder_basic(holder):
|
2015-04-22 19:31:46 +08:00
|
|
|
pm = PytestPluginManager()
|
2018-12-12 06:02:36 +08:00
|
|
|
pm.add_hookspecs(holder)
|
2014-10-01 18:20:11 +08:00
|
|
|
rec = HookRecorder(pm)
|
2014-10-04 21:49:31 +08:00
|
|
|
pm.hook.pytest_xyz(arg=123)
|
2010-10-12 19:10:39 +08:00
|
|
|
call = rec.popcall("pytest_xyz")
|
2014-10-01 18:20:11 +08:00
|
|
|
assert call.arg == 123
|
2010-10-12 19:10:39 +08:00
|
|
|
assert call._name == "pytest_xyz"
|
2018-11-23 02:05:10 +08:00
|
|
|
pytest.raises(pytest.fail.Exception, rec.popcall, "abc")
|
2014-10-04 21:49:31 +08:00
|
|
|
pm.hook.pytest_xyz_noarg()
|
2014-10-01 18:20:11 +08:00
|
|
|
call = rec.popcall("pytest_xyz_noarg")
|
|
|
|
assert call._name == "pytest_xyz_noarg"
|
|
|
|
|
2010-10-12 19:10:39 +08:00
|
|
|
|
2011-04-12 06:15:56 +08:00
|
|
|
def test_makepyfile_unicode(testdir):
|
2019-06-07 00:13:02 +08:00
|
|
|
testdir.makepyfile(chr(0xFFFD))
|
2012-09-03 16:12:30 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-09-01 04:40:05 +08:00
|
|
|
def test_makepyfile_utf8(testdir):
|
|
|
|
"""Ensure makepyfile accepts utf-8 bytes as input (#2738)"""
|
2019-06-03 06:32:00 +08:00
|
|
|
utf8_contents = """
|
2017-09-01 04:40:05 +08:00
|
|
|
def setup_function(function):
|
2019-06-05 08:48:06 +08:00
|
|
|
mixed_encoding = 'São Paulo'
|
|
|
|
""".encode()
|
2017-09-01 04:40:05 +08:00
|
|
|
p = testdir.makepyfile(utf8_contents)
|
2019-06-05 08:48:06 +08:00
|
|
|
assert "mixed_encoding = 'São Paulo'".encode() in p.read("rb")
|
2017-09-01 04:40:05 +08:00
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestInlineRunModulesCleanup:
|
2017-12-09 04:04:52 +08:00
|
|
|
def test_inline_run_test_module_not_cleaned_up(self, testdir):
|
|
|
|
test_mod = testdir.makepyfile("def test_foo(): assert True")
|
|
|
|
result = testdir.inline_run(str(test_mod))
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.OK
|
2017-12-09 04:04:52 +08:00
|
|
|
# rewrite module, now test should fail if module was re-imported
|
|
|
|
test_mod.write("def test_foo(): assert False")
|
|
|
|
result2 = testdir.inline_run(str(test_mod))
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result2.ret == ExitCode.TESTS_FAILED
|
2017-12-09 04:04:52 +08:00
|
|
|
|
|
|
|
def spy_factory(self):
|
2019-06-03 06:32:00 +08:00
|
|
|
class SysModulesSnapshotSpy:
|
2017-12-09 04:04:52 +08:00
|
|
|
instances = []
|
|
|
|
|
|
|
|
def __init__(self, preserve=None):
|
|
|
|
SysModulesSnapshotSpy.instances.append(self)
|
|
|
|
self._spy_restore_count = 0
|
|
|
|
self._spy_preserve = preserve
|
|
|
|
self.__snapshot = SysModulesSnapshot(preserve=preserve)
|
|
|
|
|
|
|
|
def restore(self):
|
|
|
|
self._spy_restore_count += 1
|
|
|
|
return self.__snapshot.restore()
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2017-12-09 04:04:52 +08:00
|
|
|
return SysModulesSnapshotSpy
|
|
|
|
|
|
|
|
def test_inline_run_taking_and_restoring_a_sys_modules_snapshot(
|
2018-05-23 22:48:46 +08:00
|
|
|
self, testdir, monkeypatch
|
|
|
|
):
|
2017-12-09 04:04:52 +08:00
|
|
|
spy_factory = self.spy_factory()
|
|
|
|
monkeypatch.setattr(pytester, "SysModulesSnapshot", spy_factory)
|
|
|
|
testdir.syspathinsert()
|
2019-07-21 02:17:30 +08:00
|
|
|
original = dict(sys.modules)
|
2017-12-09 04:04:52 +08:00
|
|
|
testdir.makepyfile(import1="# you son of a silly person")
|
|
|
|
testdir.makepyfile(import2="# my hovercraft is full of eels")
|
2018-05-23 22:48:46 +08:00
|
|
|
test_mod = testdir.makepyfile(
|
|
|
|
"""
|
2017-12-09 04:04:52 +08:00
|
|
|
import import1
|
2018-05-23 22:48:46 +08:00
|
|
|
def test_foo(): import import2"""
|
|
|
|
)
|
2017-12-09 04:04:52 +08:00
|
|
|
testdir.inline_run(str(test_mod))
|
|
|
|
assert len(spy_factory.instances) == 1
|
|
|
|
spy = spy_factory.instances[0]
|
|
|
|
assert spy._spy_restore_count == 1
|
|
|
|
assert sys.modules == original
|
|
|
|
assert all(sys.modules[x] is original[x] for x in sys.modules)
|
|
|
|
|
|
|
|
def test_inline_run_sys_modules_snapshot_restore_preserving_modules(
|
2018-05-23 22:48:46 +08:00
|
|
|
self, testdir, monkeypatch
|
|
|
|
):
|
2017-12-09 04:04:52 +08:00
|
|
|
spy_factory = self.spy_factory()
|
|
|
|
monkeypatch.setattr(pytester, "SysModulesSnapshot", spy_factory)
|
|
|
|
test_mod = testdir.makepyfile("def test_foo(): pass")
|
|
|
|
testdir.inline_run(str(test_mod))
|
|
|
|
spy = spy_factory.instances[0]
|
|
|
|
assert not spy._spy_preserve("black_knight")
|
|
|
|
assert spy._spy_preserve("zope")
|
|
|
|
assert spy._spy_preserve("zope.interface")
|
|
|
|
assert spy._spy_preserve("zopelicious")
|
|
|
|
|
|
|
|
def test_external_test_module_imports_not_cleaned_up(self, testdir):
|
|
|
|
testdir.syspathinsert()
|
|
|
|
testdir.makepyfile(imported="data = 'you son of a silly person'")
|
|
|
|
import imported
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
test_mod = testdir.makepyfile(
|
|
|
|
"""
|
2017-12-09 04:04:52 +08:00
|
|
|
def test_foo():
|
|
|
|
import imported
|
2018-05-23 22:48:46 +08:00
|
|
|
imported.data = 42"""
|
|
|
|
)
|
2017-12-09 04:04:52 +08:00
|
|
|
testdir.inline_run(str(test_mod))
|
|
|
|
assert imported.data == 42
|
|
|
|
|
|
|
|
|
2017-12-09 20:31:12 +08:00
|
|
|
def test_assert_outcomes_after_pytest_error(testdir):
|
2017-01-11 22:49:09 +08:00
|
|
|
testdir.makepyfile("def test_foo(): assert True")
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result = testdir.runpytest("--unexpected-argument")
|
2019-08-10 20:14:57 +08:00
|
|
|
with pytest.raises(ValueError, match="Pytest terminal summary report not found"):
|
2017-01-11 22:49:09 +08:00
|
|
|
result.assert_outcomes(passed=0)
|
2017-12-09 04:04:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_cwd_snapshot(tmpdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
foo = tmpdir.ensure("foo", dir=1)
|
|
|
|
bar = tmpdir.ensure("bar", dir=1)
|
2017-12-09 04:04:52 +08:00
|
|
|
foo.chdir()
|
|
|
|
snapshot = CwdSnapshot()
|
|
|
|
bar.chdir()
|
|
|
|
assert py.path.local() == bar
|
|
|
|
snapshot.restore()
|
|
|
|
assert py.path.local() == foo
|
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestSysModulesSnapshot:
|
2018-05-23 22:48:46 +08:00
|
|
|
key = "my-test-module"
|
2017-12-09 04:04:52 +08:00
|
|
|
|
|
|
|
def test_remove_added(self):
|
|
|
|
original = dict(sys.modules)
|
|
|
|
assert self.key not in sys.modules
|
|
|
|
snapshot = SysModulesSnapshot()
|
2018-05-23 22:48:46 +08:00
|
|
|
sys.modules[self.key] = "something"
|
2017-12-09 04:04:52 +08:00
|
|
|
assert self.key in sys.modules
|
|
|
|
snapshot.restore()
|
|
|
|
assert sys.modules == original
|
|
|
|
|
|
|
|
def test_add_removed(self, monkeypatch):
|
|
|
|
assert self.key not in sys.modules
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setitem(sys.modules, self.key, "something")
|
2017-12-09 04:04:52 +08:00
|
|
|
assert self.key in sys.modules
|
|
|
|
original = dict(sys.modules)
|
|
|
|
snapshot = SysModulesSnapshot()
|
|
|
|
del sys.modules[self.key]
|
|
|
|
assert self.key not in sys.modules
|
|
|
|
snapshot.restore()
|
|
|
|
assert sys.modules == original
|
|
|
|
|
|
|
|
def test_restore_reloaded(self, monkeypatch):
|
|
|
|
assert self.key not in sys.modules
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setitem(sys.modules, self.key, "something")
|
2017-12-09 04:04:52 +08:00
|
|
|
assert self.key in sys.modules
|
|
|
|
original = dict(sys.modules)
|
|
|
|
snapshot = SysModulesSnapshot()
|
2018-05-23 22:48:46 +08:00
|
|
|
sys.modules[self.key] = "something else"
|
2017-12-09 04:04:52 +08:00
|
|
|
snapshot.restore()
|
|
|
|
assert sys.modules == original
|
|
|
|
|
|
|
|
def test_preserve_modules(self, monkeypatch):
|
|
|
|
key = [self.key + str(i) for i in range(3)]
|
|
|
|
assert not any(k in sys.modules for k in key)
|
|
|
|
for i, k in enumerate(key):
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setitem(sys.modules, k, "something" + str(i))
|
2017-12-09 04:04:52 +08:00
|
|
|
original = dict(sys.modules)
|
|
|
|
|
|
|
|
def preserve(name):
|
2018-05-23 22:48:46 +08:00
|
|
|
return name in (key[0], key[1], "some-other-key")
|
2017-12-09 04:04:52 +08:00
|
|
|
|
|
|
|
snapshot = SysModulesSnapshot(preserve=preserve)
|
2018-05-23 22:48:46 +08:00
|
|
|
sys.modules[key[0]] = original[key[0]] = "something else0"
|
|
|
|
sys.modules[key[1]] = original[key[1]] = "something else1"
|
|
|
|
sys.modules[key[2]] = "something else2"
|
2017-12-09 04:04:52 +08:00
|
|
|
snapshot.restore()
|
|
|
|
assert sys.modules == original
|
|
|
|
|
|
|
|
def test_preserve_container(self, monkeypatch):
|
|
|
|
original = dict(sys.modules)
|
|
|
|
assert self.key not in original
|
|
|
|
replacement = dict(sys.modules)
|
2018-05-23 22:48:46 +08:00
|
|
|
replacement[self.key] = "life of brian"
|
2017-12-09 04:04:52 +08:00
|
|
|
snapshot = SysModulesSnapshot()
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setattr(sys, "modules", replacement)
|
2017-12-09 04:04:52 +08:00
|
|
|
snapshot.restore()
|
|
|
|
assert sys.modules is replacement
|
|
|
|
assert sys.modules == original
|
|
|
|
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("path_type", ("path", "meta_path"))
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestSysPathsSnapshot:
|
2018-05-23 22:48:46 +08:00
|
|
|
other_path = {"path": "meta_path", "meta_path": "path"}
|
2017-12-09 04:04:52 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def path(n):
|
2018-05-23 22:48:46 +08:00
|
|
|
return "my-dirty-little-secret-" + str(n)
|
2017-12-09 04:04:52 +08:00
|
|
|
|
|
|
|
def test_restore(self, monkeypatch, path_type):
|
|
|
|
other_path_type = self.other_path[path_type]
|
|
|
|
for i in range(10):
|
|
|
|
assert self.path(i) not in getattr(sys, path_type)
|
|
|
|
sys_path = [self.path(i) for i in range(6)]
|
|
|
|
monkeypatch.setattr(sys, path_type, sys_path)
|
|
|
|
original = list(sys_path)
|
|
|
|
original_other = list(getattr(sys, other_path_type))
|
|
|
|
snapshot = SysPathsSnapshot()
|
2018-07-09 07:57:18 +08:00
|
|
|
transformation = {"source": (0, 1, 2, 3, 4, 5), "target": (6, 2, 9, 7, 5, 8)}
|
2018-05-23 22:48:46 +08:00
|
|
|
assert sys_path == [self.path(x) for x in transformation["source"]]
|
2017-12-09 04:04:52 +08:00
|
|
|
sys_path[1] = self.path(6)
|
|
|
|
sys_path[3] = self.path(7)
|
|
|
|
sys_path.append(self.path(8))
|
|
|
|
del sys_path[4]
|
|
|
|
sys_path[3:3] = [self.path(9)]
|
|
|
|
del sys_path[0]
|
2018-05-23 22:48:46 +08:00
|
|
|
assert sys_path == [self.path(x) for x in transformation["target"]]
|
2017-12-09 04:04:52 +08:00
|
|
|
snapshot.restore()
|
|
|
|
assert getattr(sys, path_type) is sys_path
|
|
|
|
assert getattr(sys, path_type) == original
|
|
|
|
assert getattr(sys, other_path_type) == original_other
|
|
|
|
|
|
|
|
def test_preserve_container(self, monkeypatch, path_type):
|
|
|
|
other_path_type = self.other_path[path_type]
|
|
|
|
original_data = list(getattr(sys, path_type))
|
|
|
|
original_other = getattr(sys, other_path_type)
|
|
|
|
original_other_data = list(original_other)
|
|
|
|
new = []
|
|
|
|
snapshot = SysPathsSnapshot()
|
|
|
|
monkeypatch.setattr(sys, path_type, new)
|
|
|
|
snapshot.restore()
|
|
|
|
assert getattr(sys, path_type) is new
|
|
|
|
assert getattr(sys, path_type) == original_data
|
|
|
|
assert getattr(sys, other_path_type) is original_other
|
|
|
|
assert getattr(sys, other_path_type) == original_other_data
|
2018-07-08 08:18:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_testdir_subprocess(testdir):
|
|
|
|
testfile = testdir.makepyfile("def test_one(): pass")
|
|
|
|
assert testdir.runpytest_subprocess(testfile).ret == 0
|
2018-08-23 00:00:51 +08:00
|
|
|
|
2018-08-23 00:27:36 +08:00
|
|
|
|
2018-08-23 00:00:51 +08:00
|
|
|
def test_unicode_args(testdir):
|
2019-06-03 06:32:00 +08:00
|
|
|
result = testdir.runpytest("-k", "💩")
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2018-10-04 11:50:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_testdir_run_no_timeout(testdir):
|
|
|
|
testfile = testdir.makepyfile("def test_no_timeout(): pass")
|
2019-06-07 18:58:51 +08:00
|
|
|
assert testdir.runpytest_subprocess(testfile).ret == ExitCode.OK
|
2018-10-04 11:50:42 +08:00
|
|
|
|
|
|
|
|
2018-10-05 10:11:02 +08:00
|
|
|
def test_testdir_run_with_timeout(testdir):
|
|
|
|
testfile = testdir.makepyfile("def test_no_timeout(): pass")
|
|
|
|
|
2018-10-09 03:54:14 +08:00
|
|
|
timeout = 120
|
|
|
|
|
2018-10-05 10:11:02 +08:00
|
|
|
start = time.time()
|
2018-10-09 03:54:14 +08:00
|
|
|
result = testdir.runpytest_subprocess(testfile, timeout=timeout)
|
2018-10-05 10:11:02 +08:00
|
|
|
end = time.time()
|
|
|
|
duration = end - start
|
|
|
|
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.OK
|
2018-10-09 03:54:14 +08:00
|
|
|
assert duration < timeout
|
2018-10-05 10:11:02 +08:00
|
|
|
|
|
|
|
|
2018-10-04 11:50:42 +08:00
|
|
|
def test_testdir_run_timeout_expires(testdir):
|
|
|
|
testfile = testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import time
|
2018-10-05 05:45:30 +08:00
|
|
|
|
2018-10-04 11:50:42 +08:00
|
|
|
def test_timeout():
|
|
|
|
time.sleep(10)"""
|
|
|
|
)
|
|
|
|
with pytest.raises(testdir.TimeoutExpired):
|
2018-10-05 11:00:17 +08:00
|
|
|
testdir.runpytest_subprocess(testfile, timeout=1)
|
2019-03-15 11:14:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_linematcher_with_nonlist():
|
|
|
|
"""Test LineMatcher with regard to passing in a set (accidentally)."""
|
|
|
|
lm = LineMatcher([])
|
|
|
|
|
|
|
|
with pytest.raises(AssertionError):
|
|
|
|
lm.fnmatch_lines(set())
|
|
|
|
with pytest.raises(AssertionError):
|
|
|
|
lm.fnmatch_lines({})
|
|
|
|
lm.fnmatch_lines([])
|
|
|
|
lm.fnmatch_lines(())
|
|
|
|
|
|
|
|
assert lm._getlines({}) == {}
|
|
|
|
assert lm._getlines(set()) == set()
|
2019-04-03 18:05:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_pytester_addopts(request, monkeypatch):
|
|
|
|
monkeypatch.setenv("PYTEST_ADDOPTS", "--orig-unused")
|
|
|
|
|
|
|
|
testdir = request.getfixturevalue("testdir")
|
|
|
|
|
|
|
|
try:
|
|
|
|
assert "PYTEST_ADDOPTS" not in os.environ
|
|
|
|
finally:
|
|
|
|
testdir.finalize()
|
|
|
|
|
|
|
|
assert os.environ["PYTEST_ADDOPTS"] == "--orig-unused"
|
2019-04-06 15:55:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_run_stdin(testdir):
|
|
|
|
with pytest.raises(testdir.TimeoutExpired):
|
|
|
|
testdir.run(
|
|
|
|
sys.executable,
|
|
|
|
"-c",
|
2019-04-10 06:03:49 +08:00
|
|
|
"import sys, time; time.sleep(1); print(sys.stdin.read())",
|
2019-04-06 15:55:56 +08:00
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
timeout=0.1,
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(testdir.TimeoutExpired):
|
|
|
|
result = testdir.run(
|
|
|
|
sys.executable,
|
|
|
|
"-c",
|
|
|
|
"import sys, time; time.sleep(1); print(sys.stdin.read())",
|
|
|
|
stdin=b"input\n2ndline",
|
|
|
|
timeout=0.1,
|
|
|
|
)
|
|
|
|
|
|
|
|
result = testdir.run(
|
|
|
|
sys.executable,
|
|
|
|
"-c",
|
|
|
|
"import sys; print(sys.stdin.read())",
|
|
|
|
stdin=b"input\n2ndline",
|
|
|
|
)
|
|
|
|
assert result.stdout.lines == ["input", "2ndline"]
|
|
|
|
assert result.stderr.str() == ""
|
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_popen_stdin_pipe(testdir):
|
|
|
|
proc = testdir.popen(
|
|
|
|
[sys.executable, "-c", "import sys; print(sys.stdin.read())"],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
)
|
|
|
|
stdin = b"input\n2ndline"
|
|
|
|
stdout, stderr = proc.communicate(input=stdin)
|
|
|
|
assert stdout.decode("utf8").splitlines() == ["input", "2ndline"]
|
|
|
|
assert stderr == b""
|
|
|
|
assert proc.returncode == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_popen_stdin_bytes(testdir):
|
|
|
|
proc = testdir.popen(
|
|
|
|
[sys.executable, "-c", "import sys; print(sys.stdin.read())"],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
stdin=b"input\n2ndline",
|
|
|
|
)
|
|
|
|
stdout, stderr = proc.communicate()
|
|
|
|
assert stdout.decode("utf8").splitlines() == ["input", "2ndline"]
|
|
|
|
assert stderr == b""
|
|
|
|
assert proc.returncode == 0
|
2019-04-06 18:09:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_popen_default_stdin_stderr_and_stdin_None(testdir):
|
|
|
|
# stdout, stderr default to pipes,
|
|
|
|
# stdin can be None to not close the pipe, avoiding
|
|
|
|
# "ValueError: flush of closed file" with `communicate()`.
|
|
|
|
p1 = testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
print(sys.stdin.read()) # empty
|
|
|
|
print('stdout')
|
|
|
|
sys.stderr.write('stderr')
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
proc = testdir.popen([sys.executable, str(p1)], stdin=None)
|
|
|
|
stdout, stderr = proc.communicate(b"ignored")
|
|
|
|
assert stdout.splitlines() == [b"", b"stdout"]
|
|
|
|
assert stderr.splitlines() == [b"stderr"]
|
|
|
|
assert proc.returncode == 0
|
2019-05-24 22:46:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_spawn_uses_tmphome(testdir):
|
|
|
|
import os
|
|
|
|
|
|
|
|
tmphome = str(testdir.tmpdir)
|
|
|
|
|
|
|
|
# Does use HOME only during run.
|
|
|
|
assert os.environ.get("HOME") != tmphome
|
|
|
|
|
2019-05-24 23:49:14 +08:00
|
|
|
testdir._env_run_update["CUSTOMENV"] = "42"
|
|
|
|
|
2019-05-24 22:46:50 +08:00
|
|
|
p1 = testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
|
|
|
|
def test():
|
|
|
|
assert os.environ["HOME"] == {tmphome!r}
|
2019-05-24 23:49:14 +08:00
|
|
|
assert os.environ["CUSTOMENV"] == "42"
|
2019-05-24 22:46:50 +08:00
|
|
|
""".format(
|
|
|
|
tmphome=tmphome
|
|
|
|
)
|
|
|
|
)
|
|
|
|
child = testdir.spawn_pytest(str(p1))
|
|
|
|
out = child.read()
|
|
|
|
assert child.wait() == 0, out.decode("utf8")
|