2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2018-02-01 05:03:24 +08:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
2014-01-29 17:20:13 +08:00
|
|
|
import pytest
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2015-07-05 01:42:22 +08:00
|
|
|
from _pytest.main import EXIT_NOTESTSCOLLECTED
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class SessionTests(object):
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_basic_testitem_events(self, testdir):
|
|
|
|
tfile = testdir.makepyfile("""
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_one():
|
2008-08-16 23:26:59 +08:00
|
|
|
pass
|
|
|
|
def test_one_one():
|
|
|
|
assert 0
|
|
|
|
def test_other():
|
|
|
|
raise ValueError(23)
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-07-19 15:20:14 +08:00
|
|
|
def test_two(self, someargs):
|
|
|
|
pass
|
2008-08-16 23:26:59 +08:00
|
|
|
""")
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.inline_run(tfile)
|
|
|
|
passed, skipped, failed = reprec.listoutcomes()
|
2008-08-16 23:26:59 +08:00
|
|
|
assert len(skipped) == 0
|
|
|
|
assert len(passed) == 1
|
2012-07-20 20:16:28 +08:00
|
|
|
assert len(failed) == 3
|
2017-07-17 07:25:10 +08:00
|
|
|
|
|
|
|
def end(x):
|
|
|
|
return x.nodeid.split("::")[-1]
|
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
assert end(failed[0]) == "test_one_one"
|
|
|
|
assert end(failed[1]) == "test_other"
|
2010-11-01 06:28:31 +08:00
|
|
|
itemstarted = reprec.getcalls("pytest_itemcollected")
|
2012-07-20 20:16:28 +08:00
|
|
|
assert len(itemstarted) == 4
|
2012-07-19 15:20:14 +08:00
|
|
|
# XXX check for failing funcarg setup
|
2017-07-17 07:25:09 +08:00
|
|
|
# colreports = reprec.getcalls("pytest_collectreport")
|
|
|
|
# assert len(colreports) == 4
|
|
|
|
# assert colreports[1].report.failed
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_nested_import_error(self, testdir):
|
2009-02-27 18:18:27 +08:00
|
|
|
tfile = testdir.makepyfile("""
|
2008-08-16 23:26:59 +08:00
|
|
|
import import_fails
|
|
|
|
def test_this():
|
|
|
|
assert import_fails.a == 1
|
|
|
|
""", import_fails="""
|
2010-07-27 03:15:15 +08:00
|
|
|
import does_not_work
|
2008-08-16 23:26:59 +08:00
|
|
|
a = 1
|
|
|
|
""")
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.inline_run(tfile)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getfailedcollections()
|
|
|
|
assert len(values) == 1
|
|
|
|
out = str(values[0].longrepr)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert out.find('does_not_work') != -1
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_raises_output(self, testdir):
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.inline_runsource("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2008-08-16 23:26:59 +08:00
|
|
|
def test_raises_doesnt():
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(ValueError, int, "3")
|
2008-08-16 23:26:59 +08:00
|
|
|
""")
|
2009-05-21 20:33:09 +08:00
|
|
|
passed, skipped, failed = reprec.listoutcomes()
|
2008-08-16 23:26:59 +08:00
|
|
|
assert len(failed) == 1
|
2009-02-27 18:18:27 +08:00
|
|
|
out = failed[0].longrepr.reprcrash.message
|
2010-07-27 03:15:15 +08:00
|
|
|
if not out.find("DID NOT RAISE") != -1:
|
2009-08-30 02:04:48 +08:00
|
|
|
print(out)
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.fail("incorrect raises() output")
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_generator_yields_None(self, testdir):
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.inline_runsource("""
|
2008-08-16 23:26:59 +08:00
|
|
|
def test_1():
|
2010-07-27 03:15:15 +08:00
|
|
|
yield None
|
2008-08-16 23:26:59 +08:00
|
|
|
""")
|
2009-05-21 20:33:09 +08:00
|
|
|
failures = reprec.getfailedcollections()
|
2009-02-27 18:18:27 +08:00
|
|
|
out = failures[0].longrepr.reprcrash.message
|
2010-07-27 03:15:15 +08:00
|
|
|
i = out.find('TypeError')
|
|
|
|
assert i != -1
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_syntax_error_module(self, testdir):
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.inline_runsource("this is really not python")
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getfailedcollections()
|
|
|
|
assert len(values) == 1
|
|
|
|
out = str(values[0].longrepr)
|
2008-08-16 23:26:59 +08:00
|
|
|
assert out.find(str('not python')) != -1
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_exit_first_problem(self, testdir):
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.inline_runsource("""
|
2008-08-16 23:26:59 +08:00
|
|
|
def test_one(): assert 0
|
|
|
|
def test_two(): assert 0
|
|
|
|
""", '--exitfirst')
|
2009-05-21 20:33:09 +08:00
|
|
|
passed, skipped, failed = reprec.countoutcomes()
|
2008-08-16 23:26:59 +08:00
|
|
|
assert failed == 1
|
|
|
|
assert passed == skipped == 0
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_maxfail(self, testdir):
|
2010-05-25 22:52:09 +08:00
|
|
|
reprec = testdir.inline_runsource("""
|
|
|
|
def test_one(): assert 0
|
|
|
|
def test_two(): assert 0
|
|
|
|
def test_three(): assert 0
|
|
|
|
""", '--maxfail=2')
|
|
|
|
passed, skipped, failed = reprec.countoutcomes()
|
|
|
|
assert failed == 2
|
|
|
|
assert passed == skipped == 0
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_broken_repr(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class BrokenRepr1(object):
|
2008-08-16 23:26:59 +08:00
|
|
|
foo=0
|
|
|
|
def __repr__(self):
|
|
|
|
raise Exception("Ha Ha fooled you, I'm a broken repr().")
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestBrokenClass(object):
|
2008-08-16 23:26:59 +08:00
|
|
|
def test_explicit_bad_repr(self):
|
|
|
|
t = BrokenRepr1()
|
2010-11-13 16:05:11 +08:00
|
|
|
pytest.raises(Exception, 'repr(t)')
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2008-08-16 23:26:59 +08:00
|
|
|
def test_implicit_bad_repr1(self):
|
|
|
|
t = BrokenRepr1()
|
|
|
|
assert t.foo == 1
|
|
|
|
|
|
|
|
""")
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.inline_run(p)
|
|
|
|
passed, skipped, failed = reprec.listoutcomes()
|
2009-08-28 19:00:36 +08:00
|
|
|
assert len(failed) == 1
|
2009-02-27 18:18:27 +08:00
|
|
|
out = failed[0].longrepr.reprcrash.message
|
2017-07-17 07:25:09 +08:00
|
|
|
assert out.find("""[Exception("Ha Ha fooled you, I'm a broken repr().") raised in repr()]""") != -1 # '
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-10-14 00:41:53 +08:00
|
|
|
def test_skip_file_by_conftest(self, testdir):
|
2009-02-27 18:18:27 +08:00
|
|
|
testdir.makepyfile(conftest="""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2010-10-14 00:41:53 +08:00
|
|
|
def pytest_collect_file():
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.skip("intentional")
|
2008-09-02 16:58:14 +08:00
|
|
|
""", test_file="""
|
|
|
|
def test_one(): pass
|
|
|
|
""")
|
2010-10-14 00:41:53 +08:00
|
|
|
try:
|
|
|
|
reprec = testdir.inline_run(testdir.tmpdir)
|
2010-11-18 05:12:16 +08:00
|
|
|
except pytest.skip.Exception:
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.fail("wrong skipped caught")
|
2009-05-21 20:33:09 +08:00
|
|
|
reports = reprec.getreports("pytest_collectreport")
|
2009-04-07 18:48:57 +08:00
|
|
|
assert len(reports) == 1
|
2010-07-27 03:15:15 +08:00
|
|
|
assert reports[0].skipped
|
2008-09-02 16:58:14 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2008-08-16 23:26:59 +08:00
|
|
|
class TestNewSession(SessionTests):
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_order_of_execution(self, testdir):
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.inline_runsource("""
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2008-08-16 23:26:59 +08:00
|
|
|
def test_1():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2008-08-16 23:26:59 +08:00
|
|
|
def test_2():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(2)
|
2008-08-16 23:26:59 +08:00
|
|
|
def test_3():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1,2]
|
2017-02-17 02:41:51 +08:00
|
|
|
class Testmygroup(object):
|
2017-11-04 23:17:20 +08:00
|
|
|
reslist = values
|
2008-08-16 23:26:59 +08:00
|
|
|
def test_1(self):
|
|
|
|
self.reslist.append(1)
|
|
|
|
def test_2(self):
|
|
|
|
self.reslist.append(2)
|
|
|
|
def test_3(self):
|
|
|
|
self.reslist.append(3)
|
|
|
|
def test_4(self):
|
|
|
|
assert self.reslist == [1,2,1,2,3]
|
|
|
|
""")
|
2009-05-21 20:33:09 +08:00
|
|
|
passed, skipped, failed = reprec.countoutcomes()
|
2008-08-16 23:26:59 +08:00
|
|
|
assert failed == skipped == 0
|
|
|
|
assert passed == 7
|
2010-07-27 03:15:15 +08:00
|
|
|
# also test listnames() here ...
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_collect_only_with_various_situations(self, testdir):
|
|
|
|
p = testdir.makepyfile(
|
2008-08-16 23:26:59 +08:00
|
|
|
test_one="""
|
|
|
|
def test_one():
|
|
|
|
raise ValueError()
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestX(object):
|
2008-08-16 23:26:59 +08:00
|
|
|
def test_method_one(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class TestY(TestX):
|
|
|
|
pass
|
2010-07-27 03:15:15 +08:00
|
|
|
""",
|
2009-02-27 18:18:27 +08:00
|
|
|
test_three="xxxdsadsadsadsa",
|
|
|
|
__init__=""
|
2008-08-16 23:26:59 +08:00
|
|
|
)
|
2013-08-01 23:32:19 +08:00
|
|
|
reprec = testdir.inline_run('--collect-only', p.dirpath())
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2010-11-01 06:28:31 +08:00
|
|
|
itemstarted = reprec.getcalls("pytest_itemcollected")
|
2008-08-16 23:26:59 +08:00
|
|
|
assert len(itemstarted) == 3
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not reprec.getreports("pytest_runtest_logreport")
|
2009-05-21 20:33:09 +08:00
|
|
|
started = reprec.getcalls("pytest_collectstart")
|
|
|
|
finished = reprec.getreports("pytest_collectreport")
|
2010-07-27 03:15:15 +08:00
|
|
|
assert len(started) == len(finished)
|
2017-07-17 07:25:09 +08:00
|
|
|
assert len(started) == 7 # XXX extra TopCollector
|
2008-08-16 23:26:59 +08:00
|
|
|
colfail = [x for x in finished if x.failed]
|
|
|
|
assert len(colfail) == 1
|
|
|
|
|
2009-03-17 15:35:58 +08:00
|
|
|
def test_minus_x_import_error(self, testdir):
|
2009-03-17 18:29:45 +08:00
|
|
|
testdir.makepyfile(__init__="")
|
2009-03-17 15:35:58 +08:00
|
|
|
testdir.makepyfile(test_one="xxxx", test_two="yyyy")
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.inline_run("-x", testdir.tmpdir)
|
|
|
|
finished = reprec.getreports("pytest_collectreport")
|
2009-01-24 16:44:03 +08:00
|
|
|
colfail = [x for x in finished if x.failed]
|
|
|
|
assert len(colfail) == 1
|
|
|
|
|
2017-02-15 23:00:18 +08:00
|
|
|
def test_minus_x_overridden_by_maxfail(self, testdir):
|
2016-07-20 23:20:10 +08:00
|
|
|
testdir.makepyfile(__init__="")
|
|
|
|
testdir.makepyfile(test_one="xxxx", test_two="yyyy", test_third="zzz")
|
|
|
|
reprec = testdir.inline_run("-x", "--maxfail=2", testdir.tmpdir)
|
|
|
|
finished = reprec.getreports("pytest_collectreport")
|
|
|
|
colfail = [x for x in finished if x.failed]
|
|
|
|
assert len(colfail) == 2
|
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
|
|
|
|
def test_plugin_specify(testdir):
|
2013-10-12 21:39:22 +08:00
|
|
|
pytest.raises(ImportError, """
|
2010-10-10 19:48:48 +08:00
|
|
|
testdir.parseconfig("-p", "nqweotexistent")
|
|
|
|
""")
|
2017-07-17 07:25:09 +08:00
|
|
|
# pytest.raises(ImportError,
|
2013-09-30 19:14:14 +08:00
|
|
|
# "config.do_configure(config)"
|
2017-07-17 07:25:09 +08:00
|
|
|
# )
|
2010-10-10 19:48:48 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
def test_plugin_already_exists(testdir):
|
2010-12-06 23:54:42 +08:00
|
|
|
config = testdir.parseconfig("-p", "terminal")
|
|
|
|
assert config.option.plugins == ['terminal']
|
2015-04-22 22:33:20 +08:00
|
|
|
config._do_configure()
|
|
|
|
config._ensure_unconfigure()
|
2010-10-10 19:48:48 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
def test_exclude(testdir):
|
|
|
|
hellodir = testdir.mkdir("hello")
|
|
|
|
hellodir.join("test_hello.py").write("x y syntaxerror")
|
|
|
|
hello2dir = testdir.mkdir("hello2")
|
|
|
|
hello2dir.join("test_hello2.py").write("x y syntaxerror")
|
|
|
|
testdir.makepyfile(test_ok="def test_pass(): pass")
|
|
|
|
result = testdir.runpytest("--ignore=hello", "--ignore=hello2")
|
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2012-07-20 20:16:28 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2013-06-10 16:09:28 +08:00
|
|
|
def test_sessionfinish_with_start(testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import os
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2013-06-10 16:09:28 +08:00
|
|
|
def pytest_sessionstart():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(os.getcwd())
|
2013-06-10 16:09:28 +08:00
|
|
|
os.chdir("..")
|
|
|
|
|
|
|
|
def pytest_sessionfinish():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values[0] == os.getcwd()
|
2013-06-10 16:09:28 +08:00
|
|
|
|
|
|
|
""")
|
2013-08-01 23:32:19 +08:00
|
|
|
res = testdir.runpytest("--collect-only")
|
2015-07-05 01:42:22 +08:00
|
|
|
assert res.ret == EXIT_NOTESTSCOLLECTED
|
2018-01-18 04:02:31 +08:00
|
|
|
|
|
|
|
|
2018-02-01 05:03:24 +08:00
|
|
|
@pytest.mark.parametrize("path", ["root", "{relative}/root", "{environment}/root"])
|
|
|
|
def test_rootdir_option_arg(testdir, path):
|
|
|
|
if 'relative' in path:
|
|
|
|
path = path.format(relative=os.getcwd())
|
|
|
|
if 'environment' in path:
|
|
|
|
os.environ['PY_ROOTDIR_PATH'] = os.getcwd()
|
|
|
|
path = path.format(environment='$PY_ROOTDIR_PATH')
|
|
|
|
|
2018-01-18 04:02:31 +08:00
|
|
|
rootdir = testdir.mkdir("root")
|
2018-01-25 20:57:04 +08:00
|
|
|
rootdir.mkdir("tests")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import os
|
|
|
|
def test_one():
|
2018-02-01 05:03:24 +08:00
|
|
|
assert 1
|
2018-01-25 20:57:04 +08:00
|
|
|
""")
|
2018-01-18 04:02:31 +08:00
|
|
|
|
2018-02-01 05:03:24 +08:00
|
|
|
result = testdir.runpytest("--rootdir={}".format(os.path.expandvars(path)))
|
|
|
|
result.stdout.fnmatch_lines(['*rootdir: {}/root, inifile:*'.format(os.getcwd()), "*1 passed*"])
|
2018-01-25 20:57:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_rootdir_wrong_option_arg(testdir):
|
|
|
|
rootdir = testdir.mkdir("root")
|
|
|
|
testsdir = rootdir.mkdir("tests")
|
|
|
|
testsdir.join("test_one.py").write("def test_one():\n assert 1")
|
|
|
|
|
|
|
|
result = testdir.runpytest("--rootdir=wrong_dir")
|
|
|
|
result.stderr.fnmatch_lines(["*Directory *wrong_dir* not found. Check your '--rootdir' option.*"])
|