* move some test_pytest_terminal tests to become functional/acceptance tests

* refine pytest_namespace to not take a config object

--HG--
branch : 1.0.x
This commit is contained in:
holger krekel 2009-07-17 18:07:37 +02:00
parent e43e58afed
commit 04a98700d9
8 changed files with 74 additions and 122 deletions

View File

@ -135,8 +135,8 @@ adding global py.test helpers and functionality
If you want to make global helper functions or objects available If you want to make global helper functions or objects available
to your test code you can implement: to your test code you can implement:
def pytest_namespace(config): def pytest_namespace():
""" return dictionary with items to be made available on py.test. """ """ return dictionary with items to be made available on py.test. namespace """
All such returned items will be made available directly on All such returned items will be made available directly on
the ``py.test`` namespace. the ``py.test`` namespace.

View File

@ -9,15 +9,15 @@ py.test plugin hooks
def pytest_addoption(parser): def pytest_addoption(parser):
""" called before commandline parsing. """ """ called before commandline parsing. """
def pytest_namespace():
""" return dict of name->object which will get stored at py.test. namespace"""
def pytest_configure(config): def pytest_configure(config):
""" called after command line options have been parsed. """ called after command line options have been parsed.
and all plugins and initial conftest files been loaded. and all plugins and initial conftest files been loaded.
``config`` provides access to all such configuration values. ``config`` provides access to all such configuration values.
""" """
def pytest_namespace(config):
""" return dict of name->object to become available at py.test.*"""
def pytest_unconfigure(config): def pytest_unconfigure(config):
""" called before test process is exited. """ """ called before test process is exited. """

View File

@ -57,7 +57,7 @@ class HookRecorder:
def _makecallparser(self, method): def _makecallparser(self, method):
name = method.__name__ name = method.__name__
args, varargs, varkw, default = py.std.inspect.getargspec(method) args, varargs, varkw, default = py.std.inspect.getargspec(method)
if args[0] != "self": if args and args[0] != "self":
args.insert(0, 'self') args.insert(0, 'self')
fspec = py.std.inspect.formatargspec(args, varargs, varkw, default) fspec = py.std.inspect.formatargspec(args, varargs, varkw, default)
# we use exec because we want to have early type # we use exec because we want to have early type

View File

@ -3,7 +3,7 @@
""" """
import py import py
def pytest_namespace(config): def pytest_namespace():
mark = KeywordDecorator({}) mark = KeywordDecorator({})
return {'mark': mark} return {'mark': mark}

View File

@ -16,7 +16,7 @@ def pytest_funcarg__recwarn(request):
request.addfinalizer(warnings.finalize) request.addfinalizer(warnings.finalize)
return warnings return warnings
def pytest_namespace(config): def pytest_namespace():
return {'deprecated_call': deprecated_call} return {'deprecated_call': deprecated_call}
def deprecated_call(func, *args, **kwargs): def deprecated_call(func, *args, **kwargs):

View File

@ -16,9 +16,31 @@ from pytest_terminal import repr_pythonversion, folded_skips
def basic_run_report(item): def basic_run_report(item):
return runner.call_and_report(item, "call", log=False) return runner.call_and_report(item, "call", log=False)
class Option:
def __init__(self, verbose=False):
self.verbose = verbose
def _getcmdargs(self):
l = []
if self.verbose:
l.append('-v')
return l
def _getcmdstring(self):
return " ".join(self._getcmdargs())
def pytest_generate_tests(metafunc):
if "option" in metafunc.funcargnames:
metafunc.addcall(
id="default",
funcargs={'option': Option(verbose=False)}
)
metafunc.addcall(
id="verbose",
funcargs={'option': Option(verbose=True)}
)
class TestTerminal: class TestTerminal:
def test_pass_skip_fail(self, testdir, linecomp): def test_pass_skip_fail(self, testdir, option):
modcol = testdir.getmodulecol(""" p = testdir.makepyfile("""
import py import py
def test_ok(): def test_ok():
pass pass
@ -27,70 +49,30 @@ class TestTerminal:
def test_func(): def test_func():
assert 0 assert 0
""") """)
rep = TerminalReporter(modcol.config, file=linecomp.stringio) result = testdir.runpytest(option._getcmdstring())
rep.config.pluginmanager.register(rep) if option.verbose:
rep.config.hook.pytest_sessionstart(session=testdir.session) result.stdout.fnmatch_lines([
"*test_pass_skip_fail.py:2: *test_ok*PASS*",
for item in testdir.genitems([modcol]): "*test_pass_skip_fail.py:4: *test_skip*SKIP*",
ev = basic_run_report(item) "*test_pass_skip_fail.py:6: *test_func*FAIL*",
rep.config.hook.pytest_runtest_logreport(rep=ev) ])
linecomp.assert_contains_lines([ else:
"*test_pass_skip_fail.py .sF" result.stdout.fnmatch_lines([
"*test_pass_skip_fail.py .sF"
]) ])
rep.config.hook.pytest_sessionfinish(session=testdir.session, exitstatus=1) result.stdout.fnmatch_lines([
linecomp.assert_contains_lines([
" def test_func():", " def test_func():",
"> assert 0", "> assert 0",
"E assert 0", "E assert 0",
]) ])
def test_pass_skip_fail_verbose(self, testdir, linecomp): def test_collect_fail(self, testdir, option):
modcol = testdir.getmodulecol(""" p = testdir.makepyfile("import xyz")
import py result = testdir.runpytest(option._getcmdstring())
def test_ok(): result.stdout.fnmatch_lines([
pass "*test_collect_fail.py F*",
def test_skip():
py.test.skip("xx")
def test_func():
assert 0
""", configargs=("-v",))
rep = TerminalReporter(modcol.config, file=linecomp.stringio)
rep.config.pluginmanager.register(rep)
rep.config.hook.pytest_sessionstart(session=testdir.session)
items = modcol.collect()
rep.config.option.debug = True #
for item in items:
rep.config.hook.pytest_itemstart(item=item, node=None)
s = linecomp.stringio.getvalue().strip()
assert s.endswith(item.name)
rep.config.hook.pytest_runtest_logreport(rep=basic_run_report(item))
linecomp.assert_contains_lines([
"*test_pass_skip_fail_verbose.py:2: *test_ok*PASS*",
"*test_pass_skip_fail_verbose.py:4: *test_skip*SKIP*",
"*test_pass_skip_fail_verbose.py:6: *test_func*FAIL*",
])
rep.config.hook.pytest_sessionfinish(session=testdir.session, exitstatus=1)
linecomp.assert_contains_lines([
" def test_func():",
"> assert 0",
"E assert 0",
])
def test_collect_fail(self, testdir, linecomp):
modcol = testdir.getmodulecol("import xyz")
rep = TerminalReporter(modcol.config, file=linecomp.stringio)
rep.config.pluginmanager.register(rep)
rep.config.hook.pytest_sessionstart(session=testdir.session)
l = list(testdir.genitems([modcol]))
assert len(l) == 0
linecomp.assert_contains_lines([
"*test_collect_fail.py F*"
])
rep.config.hook.pytest_sessionfinish(session=testdir.session, exitstatus=1)
linecomp.assert_contains_lines([
"> import xyz", "> import xyz",
"E ImportError: No module named xyz" "E ImportError: No module named xyz",
]) ])
def test_internalerror(self, testdir, linecomp): def test_internalerror(self, testdir, linecomp):
@ -163,28 +145,20 @@ class TestTerminal:
"*%s*" % (modcol.config.topdir), "*%s*" % (modcol.config.topdir),
]) ])
def test_tb_option(self, testdir, linecomp): def test_tb_option(self, testdir, option):
# XXX usage of testdir p = testdir.makepyfile("""
import py
def g():
raise IndexError
def test_func():
print 6*7
g() # --calling--
""")
for tbopt in ["long", "short", "no"]: for tbopt in ["long", "short", "no"]:
print 'testing --tb=%s...' % tbopt print 'testing --tb=%s...' % tbopt
modcol = testdir.getmodulecol(""" result = testdir.runpytest('--tb=%s' % tbopt)
import py s = result.stdout.str()
def g():
raise IndexError
def test_func():
print 6*7
g() # --calling--
""", configargs=("--tb=%s" % tbopt,))
rep = TerminalReporter(modcol.config, file=linecomp.stringio)
rep.config.pluginmanager.register(rep)
rep.config.hook.pytest_sessionstart(session=testdir.session)
for item in testdir.genitems([modcol]):
rep.config.hook.pytest_runtest_logreport(
rep=basic_run_report(item))
rep.config.hook.pytest_sessionfinish(session=testdir.session, exitstatus=1)
s = linecomp.stringio.getvalue()
if tbopt == "long": if tbopt == "long":
print s
assert 'print 6*7' in s assert 'print 6*7' in s
else: else:
assert 'print 6*7' not in s assert 'print 6*7' not in s
@ -195,7 +169,6 @@ class TestTerminal:
assert 'FAILURES' not in s assert 'FAILURES' not in s
assert '--calling--' not in s assert '--calling--' not in s
assert 'IndexError' not in s assert 'IndexError' not in s
linecomp.stringio.truncate(0)
def test_show_path_before_running_test(self, testdir, linecomp): def test_show_path_before_running_test(self, testdir, linecomp):
item = testdir.getitem("def test_func(): pass") item = testdir.getitem("def test_func(): pass")
@ -245,48 +218,27 @@ class TestTerminal:
]) ])
def pseudo_keyboard_interrupt(self, testdir, linecomp, verbose=False): def test_keyboard_interrupt(self, testdir, option):
modcol = testdir.getmodulecol(""" p = testdir.makepyfile("""
def test_foobar(): def test_foobar():
assert 0 assert 0
def test_spamegg(): def test_spamegg():
import py; py.test.skip('skip me please!') import py; py.test.skip('skip me please!')
def test_interrupt_me(): def test_interrupt_me():
raise KeyboardInterrupt # simulating the user raise KeyboardInterrupt # simulating the user
""", configargs=("-v",)*verbose) """)
#""", configargs=("--showskipsummary",) + ("-v",)*verbose)
rep = TerminalReporter(modcol.config, file=linecomp.stringio) result = testdir.runpytest(option._getcmdstring())
modcol.config.pluginmanager.register(rep) result.stdout.fnmatch_lines([
modcol.config.hook.pytest_sessionstart(session=testdir.session)
try:
for item in testdir.genitems([modcol]):
modcol.config.hook.pytest_runtest_logreport(
rep=basic_run_report(item))
except KeyboardInterrupt:
excinfo = py.code.ExceptionInfo()
else:
py.test.fail("no KeyboardInterrupt??")
s = linecomp.stringio.getvalue()
if not verbose:
assert s.find("_keyboard_interrupt.py Fs") != -1
modcol.config.hook.pytest_sessionfinish(
session=testdir.session, exitstatus=2, excrepr=excinfo.getrepr())
text = linecomp.stringio.getvalue()
linecomp.assert_contains_lines([
" def test_foobar():", " def test_foobar():",
"> assert 0", "> assert 0",
"E assert 0", "E assert 0",
"*_keyboard_interrupt.py:6: KeyboardInterrupt*",
]) ])
#assert "Skipped: 'skip me please!'" in text if option.verbose:
assert "_keyboard_interrupt.py:6: KeyboardInterrupt" in text result.stdout.fnmatch_lines([
see_details = "raise KeyboardInterrupt # simulating the user" in text "*raise KeyboardInterrupt # simulating the user*",
assert see_details == verbose ])
def test_keyboard_interrupt(self, testdir, linecomp):
self.pseudo_keyboard_interrupt(testdir, linecomp)
def test_verbose_keyboard_interrupt(self, testdir, linecomp):
self.pseudo_keyboard_interrupt(testdir, linecomp, verbose=True)
def test_skip_reasons_folding(self): def test_skip_reasons_folding(self):
class longrepr: class longrepr:

View File

@ -174,7 +174,7 @@ class PluginManager(object):
if hasattr(self, '_config'): if hasattr(self, '_config'):
self.call_plugin(plugin, "pytest_addoption", parser=self._config._parser) self.call_plugin(plugin, "pytest_addoption", parser=self._config._parser)
self.call_plugin(plugin, "pytest_configure", config=self._config) self.call_plugin(plugin, "pytest_configure", config=self._config)
#dic = self.call_plugin(plugin, "pytest_namespace", config=self._config) #dic = self.call_plugin(plugin, "pytest_namespace")
#self._updateext(dic) #self._updateext(dic)
def call_plugin(self, plugin, methname, **kwargs): def call_plugin(self, plugin, methname, **kwargs):
@ -191,7 +191,7 @@ class PluginManager(object):
config.pluginmanager.register(self) config.pluginmanager.register(self)
self._config = config self._config = config
config.hook.pytest_configure(config=self._config) config.hook.pytest_configure(config=self._config)
for dic in config.hook.pytest_namespace(config=config) or []: for dic in config.hook.pytest_namespace() or []:
self._updateext(dic) self._updateext(dic)
def do_unconfigure(self, config): def do_unconfigure(self, config):

View File

@ -175,7 +175,7 @@ class TestPytestPluginInteractions:
def test_do_ext_namespace(self, testdir): def test_do_ext_namespace(self, testdir):
testdir.makeconftest(""" testdir.makeconftest("""
def pytest_namespace(config): def pytest_namespace():
return {'hello': 'world'} return {'hello': 'world'}
""") """)
p = testdir.makepyfile(""" p = testdir.makepyfile("""