2010-11-13 16:05:11 +08:00
|
|
|
import pytest, py
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
class SessionTests:
|
|
|
|
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)
|
|
|
|
def test_two(someargs):
|
|
|
|
pass
|
|
|
|
""")
|
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
|
2010-07-27 03:15:15 +08:00
|
|
|
assert len(failed) == 3
|
2010-11-06 16:58:04 +08:00
|
|
|
end = lambda x: x.nodeid.split("::")[-1]
|
|
|
|
assert end(failed[0]) == "test_one_one"
|
|
|
|
assert end(failed[1]) == "test_other"
|
|
|
|
assert end(failed[2]) == "test_two"
|
2010-11-01 06:28:31 +08:00
|
|
|
itemstarted = reprec.getcalls("pytest_itemcollected")
|
2008-08-16 23:26:59 +08:00
|
|
|
assert len(itemstarted) == 4
|
2009-05-21 20:33:09 +08:00
|
|
|
colstarted = reprec.getcalls("pytest_collectstart")
|
2010-11-06 16:58:04 +08:00
|
|
|
assert len(colstarted) == 1 + 1
|
2010-09-15 16:30:50 +08:00
|
|
|
col = colstarted[1].collector
|
2010-11-13 16:05:11 +08:00
|
|
|
assert isinstance(col, pytest.Module)
|
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)
|
|
|
|
l = reprec.getfailedcollections()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert len(l) == 1
|
2009-02-27 18:18:27 +08:00
|
|
|
out = l[0].longrepr.reprcrash.message
|
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)
|
2010-07-27 03:15:15 +08:00
|
|
|
py.test.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")
|
|
|
|
l = reprec.getfailedcollections()
|
2008-08-16 23:26:59 +08:00
|
|
|
assert len(l) == 1
|
2010-07-04 23:06:50 +08:00
|
|
|
out = str(l[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
|
2008-08-16 23:26:59 +08:00
|
|
|
class BrokenRepr1:
|
|
|
|
foo=0
|
|
|
|
def __repr__(self):
|
|
|
|
raise Exception("Ha Ha fooled you, I'm a broken repr().")
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2008-08-16 23:26:59 +08:00
|
|
|
class TestBrokenClass:
|
|
|
|
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
|
2008-08-16 23:26:59 +08:00
|
|
|
assert out.find("""[Exception("Ha Ha fooled you, I'm a broken repr().") raised in repr()]""") != -1 #'
|
|
|
|
|
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:
|
2010-10-14 00:41:53 +08:00
|
|
|
py.test.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
|
|
|
|
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("""
|
2008-08-16 23:26:59 +08:00
|
|
|
l = []
|
|
|
|
def test_1():
|
|
|
|
l.append(1)
|
|
|
|
def test_2():
|
|
|
|
l.append(2)
|
|
|
|
def test_3():
|
|
|
|
assert l == [1,2]
|
|
|
|
class Testmygroup:
|
|
|
|
reslist = l
|
|
|
|
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()
|
|
|
|
|
|
|
|
class TestX:
|
|
|
|
def test_method_one(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class TestY(TestX):
|
|
|
|
pass
|
2010-07-27 03:15:15 +08:00
|
|
|
""",
|
2008-08-16 23:26:59 +08:00
|
|
|
test_two="""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
|
|
|
pytest.skip('xxx')
|
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
|
|
|
)
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.inline_run('--collectonly', 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)
|
2010-11-06 16:58:04 +08:00
|
|
|
assert len(started) == 8 # XXX extra TopCollector
|
2008-08-16 23:26:59 +08:00
|
|
|
colfail = [x for x in finished if x.failed]
|
|
|
|
colskipped = [x for x in finished if x.skipped]
|
|
|
|
assert len(colfail) == 1
|
|
|
|
assert len(colskipped) == 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
|
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
|
|
|
|
def test_plugin_specify(testdir):
|
|
|
|
testdir.chdir()
|
2010-11-18 05:12:16 +08:00
|
|
|
config = pytest.raises(ImportError, """
|
2010-10-10 19:48:48 +08:00
|
|
|
testdir.parseconfig("-p", "nqweotexistent")
|
|
|
|
""")
|
2010-11-18 05:12:16 +08:00
|
|
|
#pytest.raises(ImportError,
|
2010-10-10 19:48:48 +08:00
|
|
|
# "config.pluginmanager.do_configure(config)"
|
|
|
|
#)
|
|
|
|
|
|
|
|
def test_plugin_already_exists(testdir):
|
2010-10-10 19:48:49 +08:00
|
|
|
config = testdir.parseconfig("-p", "session")
|
|
|
|
assert config.option.plugins == ['session']
|
2010-10-10 19:48:48 +08:00
|
|
|
config.pluginmanager.do_configure(config)
|
|
|
|
|
|
|
|
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*"])
|