2009-02-28 05:32:49 +08:00
|
|
|
import py
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
class DefaultPlugin:
|
|
|
|
""" Plugin implementing defaults and general options. """
|
|
|
|
|
2009-03-04 01:42:32 +08:00
|
|
|
def pytest_pyfunc_call(self, pyfuncitem, args, kwargs):
|
|
|
|
pyfuncitem.obj(*args, **kwargs)
|
|
|
|
return
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def pytest_collect_file(self, path, parent):
|
|
|
|
ext = path.ext
|
|
|
|
pb = path.purebasename
|
|
|
|
if pb.startswith("test_") or pb.endswith("_test") or \
|
2009-03-18 07:48:07 +08:00
|
|
|
path in parent.config.args:
|
2009-02-27 18:18:27 +08:00
|
|
|
if ext == ".py":
|
|
|
|
return parent.Module(path, parent=parent)
|
2009-03-06 06:15:42 +08:00
|
|
|
|
|
|
|
def pytest_collect_directory(self, path, parent):
|
2009-03-19 00:25:58 +08:00
|
|
|
#excludelist = parent._config.getvalue_pathlist('dir_exclude', path)
|
|
|
|
#if excludelist and path in excludelist:
|
|
|
|
# return
|
2009-03-06 06:15:42 +08:00
|
|
|
if not parent.recfilter(path):
|
2009-03-19 00:25:58 +08:00
|
|
|
# check if cmdline specified this dir or a subdir directly
|
2009-03-18 07:48:07 +08:00
|
|
|
for arg in parent.config.args:
|
2009-03-06 06:15:42 +08:00
|
|
|
if path == arg or arg.relto(path):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return
|
2009-03-19 00:25:58 +08:00
|
|
|
# not use parent.Directory here as we generally
|
|
|
|
# want dir/conftest.py to be able to
|
2009-03-06 06:15:42 +08:00
|
|
|
# define Directory(dir) already
|
2009-03-18 07:48:07 +08:00
|
|
|
Directory = parent.config.getvalue('Directory', path)
|
2009-03-06 06:15:42 +08:00
|
|
|
return Directory(path, parent=parent)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def pytest_addoption(self, parser):
|
2009-03-22 03:58:41 +08:00
|
|
|
group = parser.addgroup("general", "general test process options")
|
2009-02-27 18:18:27 +08:00
|
|
|
group._addoption('-v', '--verbose', action="count",
|
|
|
|
dest="verbose", default=0, help="increase verbosity."),
|
|
|
|
group._addoption('-x', '--exitfirst',
|
|
|
|
action="store_true", dest="exitfirst", default=False,
|
|
|
|
help="exit instantly on first error or failed test."),
|
|
|
|
group._addoption('-k',
|
|
|
|
action="store", dest="keyword", default='',
|
|
|
|
help="only run test items matching the given "
|
|
|
|
"space separated keywords. precede a keyword with '-' to negate. "
|
|
|
|
"Terminate the expression with ':' to treat a match as a signal "
|
|
|
|
"to run all subsequent tests. ")
|
|
|
|
group._addoption('-l', '--showlocals',
|
|
|
|
action="store_true", dest="showlocals", default=False,
|
2009-03-17 05:17:14 +08:00
|
|
|
help="show locals in tracebacks (disabled by default).")
|
2009-02-27 18:18:27 +08:00
|
|
|
group._addoption('--showskipsummary',
|
|
|
|
action="store_true", dest="showskipsummary", default=False,
|
2009-03-17 05:17:14 +08:00
|
|
|
help="always show summary of skipped tests")
|
|
|
|
group._addoption('--pdb',
|
2009-02-27 18:18:27 +08:00
|
|
|
action="store_true", dest="usepdb", default=False,
|
2009-03-17 05:17:14 +08:00
|
|
|
help="start pdb (the Python debugger) on errors.")
|
|
|
|
group._addoption('--tb',
|
2009-02-27 18:18:27 +08:00
|
|
|
action="store", dest="tbstyle", default='long',
|
|
|
|
type="choice", choices=['long', 'short', 'no'],
|
2009-03-17 05:17:14 +08:00
|
|
|
help="traceback verboseness (long/short/no).")
|
|
|
|
group._addoption('--fulltrace',
|
2009-02-27 18:18:27 +08:00
|
|
|
action="store_true", dest="fulltrace", default=False,
|
2009-03-17 05:17:14 +08:00
|
|
|
help="don't cut any tracebacks (default is to cut).")
|
2009-03-22 03:58:41 +08:00
|
|
|
group._addoption('-s',
|
2009-03-17 05:17:14 +08:00
|
|
|
action="store_true", dest="nocapture", default=False,
|
2009-03-22 03:58:41 +08:00
|
|
|
help="disable catching of stdout/stderr during test run.")
|
|
|
|
group._addoption('--iocapture', action="store", default="fd", metavar="method",
|
|
|
|
help="set iocapturing method: fd|sys|no.")
|
2009-03-21 01:29:08 +08:00
|
|
|
group.addoption('--basetemp', dest="basetemp", default=None, metavar="dir",
|
|
|
|
help="temporary directory for this test run.")
|
2009-03-17 18:29:45 +08:00
|
|
|
group.addoption('--boxed',
|
2009-03-17 05:17:14 +08:00
|
|
|
action="store_true", dest="boxed", default=False,
|
2009-03-21 21:54:39 +08:00
|
|
|
help="box each test run in a separate process")
|
2009-03-21 23:04:30 +08:00
|
|
|
group._addoption('-p', action="append", dest="plugin", default = [],
|
2009-03-21 21:54:39 +08:00
|
|
|
help=("load the specified plugin after command line parsing. "
|
|
|
|
"Example: '-p hello' will trigger 'import pytest_hello' "
|
|
|
|
"and instantiate 'HelloPlugin' from the module."))
|
2009-03-22 04:07:45 +08:00
|
|
|
group._addoption('-f', '--looponfail',
|
|
|
|
action="store_true", dest="looponfail", default=False,
|
2009-03-22 03:58:41 +08:00
|
|
|
help="run tests, loop on failing test set, until all pass. repeat forever.")
|
2009-03-17 05:17:14 +08:00
|
|
|
|
|
|
|
group = parser.addgroup("test process debugging")
|
|
|
|
group.addoption('--collectonly',
|
|
|
|
action="store_true", dest="collectonly",
|
|
|
|
help="only collect tests, don't execute them."),
|
|
|
|
group.addoption('--traceconfig',
|
|
|
|
action="store_true", dest="traceconfig", default=False,
|
|
|
|
help="trace considerations of conftest.py files."),
|
|
|
|
group._addoption('--nomagic',
|
|
|
|
action="store_true", dest="nomagic", default=False,
|
2009-03-22 03:28:35 +08:00
|
|
|
help="don't reinterpret asserts, no traceback cutting. ")
|
2009-03-17 05:17:14 +08:00
|
|
|
group.addoption('--debug',
|
2009-02-27 18:18:27 +08:00
|
|
|
action="store_true", dest="debug", default=False,
|
2009-03-17 05:17:14 +08:00
|
|
|
help="generate and show debugging information.")
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-03-17 05:17:14 +08:00
|
|
|
group = parser.addgroup("xplatform", "distributed/cross platform testing")
|
2009-02-27 18:18:27 +08:00
|
|
|
group._addoption('-d', '--dist',
|
|
|
|
action="store_true", dest="dist", default=False,
|
2009-03-17 05:17:14 +08:00
|
|
|
help="ad-hoc distribute tests across machines (requires conftest settings)")
|
2009-03-22 04:02:04 +08:00
|
|
|
group._addoption('-n', dest="numprocesses", default=0, metavar="numprocesses",
|
2009-03-17 05:17:14 +08:00
|
|
|
action="store", type="int",
|
|
|
|
help="number of local test processes. conflicts with --dist.")
|
2009-03-22 04:02:04 +08:00
|
|
|
group.addoption('--rsyncdir', action="append", default=[], metavar="dir1",
|
|
|
|
help="add local directory for rsync to remote test nodes.")
|
2009-03-22 03:28:35 +08:00
|
|
|
group._addoption('--tx', dest="xspec", action="append",
|
2009-03-21 01:29:08 +08:00
|
|
|
help=("add a test environment, specified in XSpec syntax. examples: "
|
|
|
|
"--tx popen//python=python2.5 --tx socket=192.168.1.102"))
|
2009-03-17 05:17:14 +08:00
|
|
|
#group._addoption('--rest',
|
|
|
|
# action='store_true', dest="restreport", default=False,
|
|
|
|
# help="restructured text output reporting."),
|
2009-02-28 05:32:49 +08:00
|
|
|
|
|
|
|
def pytest_configure(self, config):
|
|
|
|
self.setsession(config)
|
2009-03-21 21:54:39 +08:00
|
|
|
self.loadplugins(config)
|
2009-03-22 03:28:35 +08:00
|
|
|
self.fixoptions(config)
|
|
|
|
|
|
|
|
def fixoptions(self, config):
|
|
|
|
if config.getvalue("usepdb"):
|
2009-03-22 04:07:45 +08:00
|
|
|
if config.getvalue("looponfail"):
|
|
|
|
raise config.Error("--pdb incompatible with --looponfail.")
|
2009-03-22 03:28:35 +08:00
|
|
|
if config.getvalue("dist"):
|
|
|
|
raise config.Error("--pdb incomptaible with distributed testing.")
|
2009-03-21 21:54:39 +08:00
|
|
|
|
|
|
|
def loadplugins(self, config):
|
|
|
|
for name in config.getvalue("plugin"):
|
|
|
|
print "importing", name
|
|
|
|
config.pytestplugins.import_plugin(name)
|
2009-02-28 05:32:49 +08:00
|
|
|
|
|
|
|
def setsession(self, config):
|
|
|
|
val = config.getvalue
|
|
|
|
if val("collectonly"):
|
|
|
|
from py.__.test.session import Session
|
|
|
|
config.setsessionclass(Session)
|
2009-03-22 03:28:35 +08:00
|
|
|
else:
|
2009-03-22 04:07:45 +08:00
|
|
|
if val("looponfail"):
|
2009-03-22 03:28:35 +08:00
|
|
|
from py.__.test.looponfail.remote import LooponfailingSession
|
|
|
|
config.setsessionclass(LooponfailingSession)
|
|
|
|
elif val("numprocesses") or val("dist"):
|
2009-03-22 03:31:09 +08:00
|
|
|
from py.__.test.dist.dsession import DSession
|
2009-03-22 03:28:35 +08:00
|
|
|
config.setsessionclass(DSession)
|
2009-02-28 05:32:49 +08:00
|
|
|
|
2009-03-07 02:07:44 +08:00
|
|
|
def pytest_item_makereport(self, item, excinfo, when, outerr):
|
|
|
|
from py.__.test import event
|
|
|
|
return event.ItemTestReport(item, excinfo, when, outerr)
|
|
|
|
|
2009-02-28 05:32:49 +08:00
|
|
|
def test_implied_different_sessions(tmpdir):
|
|
|
|
def x(*args):
|
|
|
|
config = py.test.config._reparse([tmpdir] + list(args))
|
|
|
|
try:
|
2009-03-01 21:56:29 +08:00
|
|
|
config.pytestplugins.do_configure(config)
|
2009-02-28 05:32:49 +08:00
|
|
|
except ValueError:
|
|
|
|
return Exception
|
|
|
|
return getattr(config._sessionclass, '__name__', None)
|
|
|
|
assert x() == None
|
|
|
|
assert x('--dist') == 'DSession'
|
|
|
|
assert x('-n3') == 'DSession'
|
|
|
|
assert x('-f') == 'LooponfailingSession'
|
2009-03-22 03:28:35 +08:00
|
|
|
assert x('--dist', '--collectonly') == 'Session'
|
2009-03-21 21:54:39 +08:00
|
|
|
|
|
|
|
def test_generic(plugintester):
|
|
|
|
plugintester.apicheck(DefaultPlugin)
|
|
|
|
|
|
|
|
def test_plugin_specify(testdir):
|
|
|
|
testdir.chdir()
|
|
|
|
config = testdir.parseconfig("-p", "nqweotexistent")
|
|
|
|
py.test.raises(ImportError,
|
|
|
|
"config.pytestplugins.do_configure(config)"
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_plugin_already_exists(testdir):
|
2009-03-21 23:04:30 +08:00
|
|
|
config = testdir.parseconfig("-p", "default")
|
2009-03-21 21:54:39 +08:00
|
|
|
assert config.option.plugin == ['default']
|
2009-03-21 23:04:30 +08:00
|
|
|
config.pytestplugins.do_configure(config)
|
2009-03-22 03:28:35 +08:00
|
|
|
|
|
|
|
def test_conflict_options():
|
|
|
|
def check_conflict_option(opts):
|
|
|
|
print "testing if options conflict:", " ".join(opts)
|
|
|
|
config = py.test.config._reparse(opts)
|
|
|
|
py.test.raises(config.Error,
|
|
|
|
"config.pytestplugins.do_configure(config)")
|
|
|
|
conflict_options = (
|
2009-03-22 04:07:45 +08:00
|
|
|
'--looponfail --pdb',
|
2009-03-22 03:28:35 +08:00
|
|
|
'--dist --pdb',
|
|
|
|
)
|
|
|
|
for spec in conflict_options:
|
|
|
|
opts = spec.split()
|
|
|
|
yield check_conflict_option, opts
|