2010-11-18 05:12:16 +08:00
|
|
|
import py, pytest
|
2009-09-06 22:59:39 +08:00
|
|
|
import os
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.resultlog import generic_path, ResultLog, \
|
2010-07-02 19:01:21 +08:00
|
|
|
pytest_configure, pytest_unconfigure
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.session import Node, Item, FSCollector
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2009-12-30 23:18:59 +08:00
|
|
|
def test_generic_path(testdir):
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.session import Session
|
2010-01-03 08:02:44 +08:00
|
|
|
config = testdir.parseconfig()
|
2010-11-07 17:19:58 +08:00
|
|
|
session = Session(config)
|
|
|
|
p1 = Node('a', config=config, session=session)
|
2010-01-03 08:02:44 +08:00
|
|
|
#assert p1.fspath is None
|
2009-09-06 22:59:39 +08:00
|
|
|
p2 = Node('B', parent=p1)
|
|
|
|
p3 = Node('()', parent = p2)
|
|
|
|
item = Item('c', parent = p3)
|
|
|
|
|
|
|
|
res = generic_path(item)
|
|
|
|
assert res == 'a.B().c'
|
|
|
|
|
2010-11-07 17:19:58 +08:00
|
|
|
p0 = FSCollector('proj/test', config=config, session=session)
|
2009-09-06 22:59:39 +08:00
|
|
|
p1 = FSCollector('proj/test/a', parent=p0)
|
|
|
|
p2 = Node('B', parent=p1)
|
|
|
|
p3 = Node('()', parent = p2)
|
|
|
|
p4 = Node('c', parent=p3)
|
|
|
|
item = Item('[1]', parent = p4)
|
|
|
|
|
|
|
|
res = generic_path(item)
|
|
|
|
assert res == 'test/a:B().c[1]'
|
|
|
|
|
|
|
|
def test_write_log_entry():
|
2009-09-17 21:31:35 +08:00
|
|
|
reslog = ResultLog(None, None)
|
2009-09-06 22:59:39 +08:00
|
|
|
reslog.logfile = py.io.TextIO()
|
2010-07-27 03:15:15 +08:00
|
|
|
reslog.write_log_entry('name', '.', '')
|
2009-09-06 22:59:39 +08:00
|
|
|
entry = reslog.logfile.getvalue()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert entry[-1] == '\n'
|
2009-09-06 22:59:39 +08:00
|
|
|
entry_lines = entry.splitlines()
|
|
|
|
assert len(entry_lines) == 1
|
|
|
|
assert entry_lines[0] == '. name'
|
|
|
|
|
|
|
|
reslog.logfile = py.io.TextIO()
|
2010-07-27 03:15:15 +08:00
|
|
|
reslog.write_log_entry('name', 's', 'Skipped')
|
2009-09-06 22:59:39 +08:00
|
|
|
entry = reslog.logfile.getvalue()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert entry[-1] == '\n'
|
2009-09-06 22:59:39 +08:00
|
|
|
entry_lines = entry.splitlines()
|
|
|
|
assert len(entry_lines) == 2
|
|
|
|
assert entry_lines[0] == 's name'
|
|
|
|
assert entry_lines[1] == ' Skipped'
|
|
|
|
|
|
|
|
reslog.logfile = py.io.TextIO()
|
2010-07-27 03:15:15 +08:00
|
|
|
reslog.write_log_entry('name', 's', 'Skipped\n')
|
2009-09-06 22:59:39 +08:00
|
|
|
entry = reslog.logfile.getvalue()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert entry[-1] == '\n'
|
2009-09-06 22:59:39 +08:00
|
|
|
entry_lines = entry.splitlines()
|
|
|
|
assert len(entry_lines) == 2
|
|
|
|
assert entry_lines[0] == 's name'
|
|
|
|
assert entry_lines[1] == ' Skipped'
|
|
|
|
|
|
|
|
reslog.logfile = py.io.TextIO()
|
|
|
|
longrepr = ' tb1\n tb 2\nE tb3\nSome Error'
|
|
|
|
reslog.write_log_entry('name', 'F', longrepr)
|
|
|
|
entry = reslog.logfile.getvalue()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert entry[-1] == '\n'
|
2009-09-06 22:59:39 +08:00
|
|
|
entry_lines = entry.splitlines()
|
|
|
|
assert len(entry_lines) == 5
|
|
|
|
assert entry_lines[0] == 'F name'
|
|
|
|
assert entry_lines[1:] == [' '+line for line in longrepr.splitlines()]
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
class TestWithFunctionIntegration:
|
|
|
|
# XXX (hpk) i think that the resultlog plugin should
|
2010-07-27 03:15:15 +08:00
|
|
|
# provide a Parser object so that one can remain
|
|
|
|
# ignorant regarding formatting details.
|
2009-09-06 22:59:39 +08:00
|
|
|
def getresultlog(self, testdir, arg):
|
|
|
|
resultlog = testdir.tmpdir.join("resultlog")
|
|
|
|
testdir.plugins.append("resultlog")
|
|
|
|
args = ["--resultlog=%s" % resultlog] + [arg]
|
|
|
|
testdir.runpytest(*args)
|
|
|
|
return [x for x in resultlog.readlines(cr=0) if x]
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_collection_report(self, testdir):
|
|
|
|
ok = testdir.makepyfile(test_collection_ok="")
|
2010-11-18 05:12:16 +08:00
|
|
|
skip = testdir.makepyfile(test_collection_skip=
|
|
|
|
"import pytest ; pytest.skip('hello')")
|
2009-09-06 22:59:39 +08:00
|
|
|
fail = testdir.makepyfile(test_collection_fail="XXX")
|
2010-07-27 03:15:15 +08:00
|
|
|
lines = self.getresultlog(testdir, ok)
|
2009-09-06 22:59:39 +08:00
|
|
|
assert not lines
|
|
|
|
|
|
|
|
lines = self.getresultlog(testdir, skip)
|
|
|
|
assert len(lines) == 2
|
|
|
|
assert lines[0].startswith("S ")
|
|
|
|
assert lines[0].endswith("test_collection_skip.py")
|
|
|
|
assert lines[1].startswith(" ")
|
2010-11-06 06:37:31 +08:00
|
|
|
assert lines[1].endswith("test_collection_skip.py:1: Skipped: hello")
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
lines = self.getresultlog(testdir, fail)
|
|
|
|
assert lines
|
|
|
|
assert lines[0].startswith("F ")
|
|
|
|
assert lines[0].endswith("test_collection_fail.py"), lines[0]
|
|
|
|
for x in lines[1:]:
|
|
|
|
assert x.startswith(" ")
|
|
|
|
assert "XXX" in "".join(lines[1:])
|
|
|
|
|
|
|
|
def test_log_test_outcomes(self, testdir):
|
|
|
|
mod = testdir.makepyfile(test_mod="""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_pass(): pass
|
2010-11-18 05:12:16 +08:00
|
|
|
def test_skip(): pytest.skip("hello")
|
2009-09-17 21:31:35 +08:00
|
|
|
def test_fail(): raise ValueError("FAIL")
|
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.xfail
|
2009-09-17 21:31:35 +08:00
|
|
|
def test_xfail(): raise ValueError("XFAIL")
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.xfail
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_xpass(): pass
|
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
""")
|
|
|
|
lines = self.getresultlog(testdir, mod)
|
|
|
|
assert len(lines) >= 3
|
|
|
|
assert lines[0].startswith(". ")
|
|
|
|
assert lines[0].endswith("test_pass")
|
|
|
|
assert lines[1].startswith("s "), lines[1]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert lines[1].endswith("test_skip")
|
2009-09-06 22:59:39 +08:00
|
|
|
assert lines[2].find("hello") != -1
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
assert lines[3].startswith("F ")
|
|
|
|
assert lines[3].endswith("test_fail")
|
2009-09-17 21:31:35 +08:00
|
|
|
tb = "".join(lines[4:8])
|
|
|
|
assert tb.find('raise ValueError("FAIL")') != -1
|
|
|
|
|
|
|
|
assert lines[8].startswith('x ')
|
|
|
|
tb = "".join(lines[8:14])
|
|
|
|
assert tb.find('raise ValueError("XFAIL")') != -1
|
|
|
|
|
2010-05-20 20:35:13 +08:00
|
|
|
assert lines[14].startswith('X ')
|
2009-09-17 21:31:35 +08:00
|
|
|
assert len(lines) == 15
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
def test_internal_exception(self):
|
|
|
|
# they are produced for example by a teardown failing
|
|
|
|
# at the end of the run
|
|
|
|
try:
|
|
|
|
raise ValueError
|
|
|
|
except ValueError:
|
|
|
|
excinfo = py.code.ExceptionInfo()
|
2010-07-27 03:15:15 +08:00
|
|
|
reslog = ResultLog(None, py.io.TextIO())
|
2009-09-06 22:59:39 +08:00
|
|
|
reslog.pytest_internalerror(excinfo.getrepr())
|
|
|
|
entry = reslog.logfile.getvalue()
|
|
|
|
entry_lines = entry.splitlines()
|
|
|
|
|
|
|
|
assert entry_lines[0].startswith('! ')
|
2010-04-28 21:24:38 +08:00
|
|
|
assert os.path.basename(__file__)[:-9] in entry_lines[0] #.pyc/class
|
2009-09-06 22:59:39 +08:00
|
|
|
assert entry_lines[-1][0] == ' '
|
2010-07-27 03:15:15 +08:00
|
|
|
assert 'ValueError' in entry
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
def test_generic(testdir, LineMatcher):
|
|
|
|
testdir.plugins.append("resultlog")
|
|
|
|
testdir.makepyfile("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
def test_fail():
|
|
|
|
assert 0
|
|
|
|
def test_skip():
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.skip("")
|
|
|
|
@pytest.mark.xfail
|
2009-09-17 21:31:35 +08:00
|
|
|
def test_xfail():
|
|
|
|
assert 0
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.xfail(run=False)
|
2010-05-03 04:13:16 +08:00
|
|
|
def test_xfail_norun():
|
|
|
|
assert 0
|
2009-09-06 22:59:39 +08:00
|
|
|
""")
|
|
|
|
testdir.runpytest("--resultlog=result.log")
|
|
|
|
lines = testdir.tmpdir.join("result.log").readlines(cr=0)
|
|
|
|
LineMatcher(lines).fnmatch_lines([
|
2010-07-27 03:15:15 +08:00
|
|
|
". *:test_pass",
|
|
|
|
"F *:test_fail",
|
2009-09-17 21:31:35 +08:00
|
|
|
"s *:test_skip",
|
2010-07-27 03:15:15 +08:00
|
|
|
"x *:test_xfail",
|
|
|
|
"x *:test_xfail_norun",
|
2009-09-06 22:59:39 +08:00
|
|
|
])
|
2010-07-02 19:01:21 +08:00
|
|
|
|
|
|
|
def test_no_resultlog_on_slaves(testdir):
|
|
|
|
config = testdir.parseconfig("-p", "resultlog", "--resultlog=resultlog")
|
|
|
|
|
|
|
|
assert not hasattr(config, '_resultlog')
|
|
|
|
pytest_configure(config)
|
|
|
|
assert hasattr(config, '_resultlog')
|
|
|
|
pytest_unconfigure(config)
|
|
|
|
assert not hasattr(config, '_resultlog')
|
|
|
|
|
|
|
|
config.slaveinput = {}
|
|
|
|
pytest_configure(config)
|
|
|
|
assert not hasattr(config, '_resultlog')
|
|
|
|
pytest_unconfigure(config)
|
|
|
|
assert not hasattr(config, '_resultlog')
|
|
|
|
|