[svn r62981] allowing conftest to set default values for options

--HG--
branch : trunk
This commit is contained in:
hpk 2009-03-17 07:10:40 +01:00
parent b5a1f95856
commit 771ce92fdf
7 changed files with 63 additions and 24 deletions

View File

@ -63,6 +63,12 @@ class Config(object):
elif not opt.type and opt.action in ("store_true", "store_false"): elif not opt.type and opt.action in ("store_true", "store_false"):
val = eval(val) val = eval(val)
opt.default = val opt.default = val
else:
name = "pytest_option_" + opt.dest
try:
opt.default = self._conftest.rget(name)
except (ValueError, KeyError):
pass
if not hasattr(self.option, opt.dest): if not hasattr(self.option, opt.dest):
setattr(self.option, opt.dest, opt.default) setattr(self.option, opt.dest, opt.default)

View File

@ -60,16 +60,12 @@ class DSession(Session):
if config.option.numprocesses: if config.option.numprocesses:
return return
try: try:
config.getvalue('dist_hosts') config.getvalue('hosts')
except KeyError: except KeyError:
print "Don't know where to distribute tests to. You may want" print "Please specify hosts for distribution of tests:"
print "to specify either a number of local processes to start" print "cmdline: --hosts=host1,host2,..."
print "with '--numprocesses=NUM' or specify 'dist_hosts' in a local" print "conftest.py: pytest_option_hosts=['host1','host2',]"
print "conftest.py file, for example:" print "environment: PYTEST_OPTION_HOSTS=host1,host2,host3"
print
print " dist_hosts = ['localhost'] * 4 # for 3 processors"
print " dist_hosts = ['you@remote.com', '...'] # for testing on ssh accounts"
print " # with your remote ssh accounts"
print print
print "see also: http://codespeak.net/py/current/doc/test.html#automated-distributed-testing" print "see also: http://codespeak.net/py/current/doc/test.html#automated-distributed-testing"
raise SystemExit raise SystemExit

View File

@ -6,7 +6,7 @@ from py.__.test import event
def getconfighosts(config): def getconfighosts(config):
if config.option.numprocesses: if config.option.numprocesses:
hosts = ['localhost'] * config.option.numprocesses hosts = ['popen'] * config.option.numprocesses
else: else:
hosts = config.option.hosts hosts = config.option.hosts
if not hosts: if not hosts:

View File

@ -300,17 +300,17 @@ class TestDSession:
remaining = session.filteritems(items) remaining = session.filteritems(items)
assert remaining == [] assert remaining == []
evname, ev = evrec.events[-1] event = evrec.events[-1]
assert evname == "deselected" assert event.name == "deselected"
assert ev.items == items assert event.args[0].items == items
modcol._config.option.keyword = "test_fail" modcol._config.option.keyword = "test_fail"
remaining = session.filteritems(items) remaining = session.filteritems(items)
assert remaining == [items[0]] assert remaining == [items[0]]
evname, ev = evrec.events[-1] event = evrec.events[-1]
assert evname == "deselected" assert event.name == "deselected"
assert ev.items == [items[1]] assert event.args[0].items == [items[1]]
def test_hostdown_shutdown_after_completion(self, testdir): def test_hostdown_shutdown_after_completion(self, testdir):
item = testdir.getitem("def test_func(): pass") item = testdir.getitem("def test_func(): pass")

View File

@ -236,6 +236,11 @@ class TmpTestdir:
def runpytest(self, *args): def runpytest(self, *args):
return self.runpybin("py.test", *args) return self.runpybin("py.test", *args)
class Event:
def __init__(self, name, args, kwargs):
self.name = name
self.args = args
self.kwargs = kwargs
class EventRecorder(object): class EventRecorder(object):
def __init__(self, pyplugins, debug=False): # True): def __init__(self, pyplugins, debug=False): # True):
@ -249,26 +254,27 @@ class EventRecorder(object):
return return
if self.debug: if self.debug:
print "[event: %s]: %s **%s" %(name, ", ".join(map(str, args)), kwargs,) print "[event: %s]: %s **%s" %(name, ", ".join(map(str, args)), kwargs,)
self.events.append((name,) + tuple(args)) self.events.append(Event(name, args, kwargs))
def get(self, cls): def get(self, cls):
l = [] l = []
for name, value in self.events: for event in self.events:
value = event.args[0]
if isinstance(value, cls): if isinstance(value, cls):
l.append(value) l.append(value)
return l return l
def getnamed(self, *names): def getnamed(self, *names):
l = [] l = []
for evname, event in self.events: for event in self.events:
if evname in names: if event.name in names:
l.append(event) l.append(event.args[0])
return l return l
def getfirstnamed(self, name): def getfirstnamed(self, name):
for evname, event in self.events: for event in self.events:
if evname == name: if event.name == name:
return event return event.args[0]
def getfailures(self, names='itemtestreport collectionreport'): def getfailures(self, names='itemtestreport collectionreport'):
l = [] l = []

View File

@ -252,6 +252,31 @@ class TestPyTest:
]) ])
assert result.ret == 1 assert result.ret == 1
def test_dist_testing_conftest_specified(self, testdir):
p1 = testdir.makepyfile("""
import py
def test_fail0():
assert 0
def test_fail1():
raise ValueError()
def test_ok():
pass
def test_skip():
py.test.skip("hello")
""",
)
testdir.makeconftest("""
pytest_option_hosts='popen,popen,popen'
""")
result = testdir.runpytest(p1, '-d')
result.stdout.fnmatch_lines([
"HOSTUP: popen*Python*",
#"HOSTUP: localhost*Python*",
#"HOSTUP: localhost*Python*",
"*2 failed, 1 passed, 1 skipped*",
])
assert result.ret == 1
def test_dist_tests_with_crash(self, testdir): def test_dist_tests_with_crash(self, testdir):
if not hasattr(py.std.os, 'kill'): if not hasattr(py.std.os, 'kill'):
py.test.skip("no os.kill") py.test.skip("no os.kill")

View File

@ -40,6 +40,12 @@ class TestConfigCmdlineParsing:
group.addoption("--option4", action="store", type="int") group.addoption("--option4", action="store", type="int")
assert group.options[3].default == ("NO", "DEFAULT") assert group.options[3].default == ("NO", "DEFAULT")
def test_parser_addoption_default_conftest(self, testdir, monkeypatch):
import os
testdir.makeconftest("pytest_option_verbose=True")
config = testdir.parseconfig()
assert config.option.verbose
def test_config_cmdline_options_only_lowercase(self, testdir): def test_config_cmdline_options_only_lowercase(self, testdir):
testdir.makepyfile(conftest=""" testdir.makepyfile(conftest="""
import py import py