2008-08-16 23:26:59 +08:00
|
|
|
import py
|
|
|
|
|
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-09-26 22:23:44 +08:00
|
|
|
assert failed[0].nodenames[-1] == "test_one_one"
|
|
|
|
assert failed[1].nodenames[-1] == "test_other"
|
|
|
|
assert failed[2].nodenames[-1] == "test_two"
|
2010-09-15 16:30:50 +08:00
|
|
|
itemstarted = reprec.getcalls("pytest_log_itemcollect")
|
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-09-15 16:30:50 +08:00
|
|
|
assert len(colstarted) == 1 + 1 # XXX ExtraTopCollector
|
|
|
|
col = colstarted[1].collector
|
2008-08-16 23:26:59 +08:00
|
|
|
assert isinstance(col, py.test.collect.Module)
|
|
|
|
|
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("""
|
2008-08-16 23:26:59 +08:00
|
|
|
import py
|
|
|
|
def test_raises_doesnt():
|
|
|
|
py.test.raises(ValueError, int, "3")
|
|
|
|
""")
|
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("""
|
2008-08-16 23:26:59 +08:00
|
|
|
import py
|
|
|
|
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()
|
|
|
|
py.test.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 #'
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_skip_by_conftest_directory(self, testdir):
|
|
|
|
testdir.makepyfile(conftest="""
|
2008-09-02 16:58:14 +08:00
|
|
|
import py
|
|
|
|
class Directory(py.test.collect.Directory):
|
|
|
|
def collect(self):
|
|
|
|
py.test.skip("intentional")
|
|
|
|
""", test_file="""
|
|
|
|
def test_one(): pass
|
|
|
|
""")
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.inline_run(testdir.tmpdir)
|
|
|
|
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="""
|
|
|
|
import py
|
|
|
|
py.test.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-09-15 16:30:50 +08:00
|
|
|
itemstarted = reprec.getcalls("pytest_log_itemcollect")
|
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-09-15 16:30:50 +08:00
|
|
|
assert len(started) == 8 + 1 # 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
|
|
|
|
|