2017-09-01 04:40:05 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
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
|
2015-04-22 19:31:46 +08:00
|
|
|
from _pytest.config import PytestPluginManager
|
2015-04-21 10:18:04 +08:00
|
|
|
from _pytest.main import EXIT_OK, EXIT_TESTSFAILED
|
|
|
|
|
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
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class rep(object):
|
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]
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class rep(object):
|
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"
|
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
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_testdir_runs_with_plugin(testdir):
|
|
|
|
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
|
|
|
|
""")
|
|
|
|
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
|
|
|
|
|
|
|
def make_holder():
|
2017-02-17 02:41:51 +08:00
|
|
|
class apiclass(object):
|
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
|
|
|
|
|
|
|
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()
|
|
|
|
pm.addhooks(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"
|
2014-10-01 18:20:11 +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):
|
|
|
|
global unichr
|
|
|
|
try:
|
|
|
|
unichr(65)
|
|
|
|
except NameError:
|
|
|
|
unichr = chr
|
|
|
|
testdir.makepyfile(unichr(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)"""
|
|
|
|
utf8_contents = u"""
|
|
|
|
def setup_function(function):
|
|
|
|
mixed_encoding = u'São Paulo'
|
|
|
|
""".encode('utf-8')
|
|
|
|
p = testdir.makepyfile(utf8_contents)
|
|
|
|
assert u"mixed_encoding = u'São Paulo'".encode('utf-8') in p.read('rb')
|
|
|
|
|
|
|
|
|
2015-04-21 10:18:04 +08:00
|
|
|
def test_inline_run_clean_modules(testdir):
|
|
|
|
test_mod = testdir.makepyfile("def test_foo(): assert True")
|
|
|
|
result = testdir.inline_run(str(test_mod))
|
|
|
|
assert result.ret == EXIT_OK
|
|
|
|
# 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))
|
|
|
|
assert result2.ret == EXIT_TESTSFAILED
|
2017-01-11 22:49:09 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
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")
|
|
|
|
|
|
|
|
result = testdir.runpytest('--unexpected-argument')
|
|
|
|
with pytest.raises(ValueError, message="Pytest terminal report not found"):
|
|
|
|
result.assert_outcomes(passed=0)
|