2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2013-10-12 21:39:22 +08:00
|
|
|
import os
|
|
|
|
from _pytest.pytester import HookRecorder
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.core import PluginManager
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
def test_reportrecorder(testdir):
|
|
|
|
item = testdir.getitem("def test_func(): pass")
|
2009-12-29 19:36:17 +08:00
|
|
|
recorder = testdir.getreportrecorder(item.config)
|
|
|
|
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")
|
2009-09-06 22:59:39 +08:00
|
|
|
class rep:
|
|
|
|
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]
|
|
|
|
|
|
|
|
class rep:
|
|
|
|
excinfo = None
|
|
|
|
passed = False
|
|
|
|
failed = False
|
|
|
|
skipped = True
|
2010-07-27 03:15:15 +08:00
|
|
|
when = "call"
|
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)
|
2010-11-18 05:12:16 +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()
|
|
|
|
assert config2 != config1
|
2014-01-18 19:31:33 +08:00
|
|
|
assert config1 != pytest.config
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
def test_testdir_runs_with_plugin(testdir):
|
|
|
|
testdir.makepyfile("""
|
2010-07-27 03:15:15 +08:00
|
|
|
pytest_plugins = "pytest_pytester"
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_hello(testdir):
|
|
|
|
assert 1
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
2010-04-29 06:12:38 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2009-09-06 22:59:39 +08:00
|
|
|
"*1 passed*"
|
|
|
|
])
|
|
|
|
|
2010-10-12 19:10:39 +08:00
|
|
|
def test_hookrecorder_basic():
|
2010-10-13 17:12:27 +08:00
|
|
|
rec = HookRecorder(PluginManager())
|
2010-10-12 19:10:39 +08:00
|
|
|
class ApiClass:
|
|
|
|
def pytest_xyz(self, arg):
|
|
|
|
"x"
|
|
|
|
rec.start_recording(ApiClass)
|
|
|
|
rec.hook.pytest_xyz(arg=123)
|
|
|
|
call = rec.popcall("pytest_xyz")
|
|
|
|
assert call.arg == 123
|
|
|
|
assert call._name == "pytest_xyz"
|
2010-11-22 22:20:18 +08:00
|
|
|
pytest.raises(pytest.fail.Exception, "rec.popcall('abc')")
|
2010-10-12 19:10:39 +08:00
|
|
|
|
|
|
|
def test_hookrecorder_basic_no_args_hook():
|
2010-10-13 17:12:27 +08:00
|
|
|
rec = HookRecorder(PluginManager())
|
2010-10-12 19:10:39 +08:00
|
|
|
apimod = type(os)('api')
|
|
|
|
def pytest_xyz():
|
|
|
|
"x"
|
|
|
|
apimod.pytest_xyz = pytest_xyz
|
|
|
|
rec.start_recording(apimod)
|
|
|
|
rec.hook.pytest_xyz()
|
|
|
|
call = rec.popcall("pytest_xyz")
|
|
|
|
assert call._name == "pytest_xyz"
|
|
|
|
|
|
|
|
def test_functional(testdir, linecomp):
|
|
|
|
reprec = testdir.inline_runsource("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.core import HookRelay, PluginManager
|
2010-10-12 19:10:39 +08:00
|
|
|
pytest_plugins="pytester"
|
|
|
|
def test_func(_pytest):
|
|
|
|
class ApiClass:
|
|
|
|
def pytest_xyz(self, arg): "x"
|
2013-09-30 19:14:14 +08:00
|
|
|
hook = HookRelay([ApiClass], PluginManager())
|
2010-10-12 19:10:39 +08:00
|
|
|
rec = _pytest.gethookrecorder(hook)
|
|
|
|
class Plugin:
|
|
|
|
def pytest_xyz(self, arg):
|
|
|
|
return arg + 1
|
2010-10-13 17:12:27 +08:00
|
|
|
rec._pluginmanager.register(Plugin())
|
2010-10-12 19:10:39 +08:00
|
|
|
res = rec.hook.pytest_xyz(arg=41)
|
|
|
|
assert res == [42]
|
|
|
|
""")
|
|
|
|
reprec.assertoutcome(passed=1)
|
2011-04-12 06:15:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_makepyfile_unicode(testdir):
|
|
|
|
global unichr
|
|
|
|
try:
|
|
|
|
unichr(65)
|
|
|
|
except NameError:
|
|
|
|
unichr = chr
|
|
|
|
testdir.makepyfile(unichr(0xfffd))
|
2012-09-03 16:12:30 +08:00
|
|
|
|
|
|
|
def test_inprocess_plugins(testdir):
|
|
|
|
class Plugin(object):
|
|
|
|
configured = False
|
|
|
|
def pytest_configure(self, config):
|
|
|
|
self.configured = True
|
|
|
|
plugin = Plugin()
|
|
|
|
testdir.inprocess_run([], [plugin])
|
|
|
|
|
|
|
|
assert plugin.configured
|