reduce usage of the global py.test.config which maybe should die or become less global at some point (along with py.test.ensuretemp)

--HG--
branch : trunk
This commit is contained in:
holger krekel 2010-01-13 18:04:58 +01:00
parent 9da1ba40ed
commit 030986dcc4
5 changed files with 66 additions and 68 deletions

View File

@ -51,14 +51,23 @@ but a remote one fail because the tests directory
does not contain an "__init__.py". Either give does not contain an "__init__.py". Either give
an error or make it work without the __init__.py an error or make it work without the __init__.py
deprecate ensuretemp / introduce funcargs to setup method consider globals: py.test.ensuretemp and config
-------------------------------------------------------------- --------------------------------------------------------------
tags: experimental-wish 1.2 tags: experimental-wish 1.2
The remaining uses of py.test.ensuretemp within the py-test base consider deprecating py.test.ensuretemp and py.test.config
itself are for setup methods. Also users have expressed the to further reduce py.test globality. Also consider
wish to have funcargs available to setup functions. Experiment having py.test.config and ensuretemp coming from
with allowing funcargs there and finalizing deprecating py.test.ensuretemp. a plugin rather than being there from the start.
consider allowing funcargs to setup methods
--------------------------------------------------------------
tags: experimental-wish 1.2
Users have expressed the wish to have funcargs available to setup
functions. Experiment with allowing funcargs there - it might
also help to make the py.test.ensuretemp and config deprecation.
outsource figleaf plugin outsource figleaf plugin
--------------------------------------- ---------------------------------------

View File

@ -19,8 +19,8 @@ class TestGeneralUsage:
""") """)
testdir.makepyfile(test_one=""" testdir.makepyfile(test_one="""
import py import py
def test_option(): def test_option(pytestconfig):
assert py.test.config.option.xyz == "123" assert pytestconfig.option.xyz == "123"
""") """)
result = testdir.runpytest("-p", "xyz", "--xyz=123") result = testdir.runpytest("-p", "xyz", "--xyz=123")
assert result.ret == 0 assert result.ret == 0

View File

@ -44,46 +44,69 @@ def test_unicode_encoding():
tw.line(msg) tw.line(msg)
assert l[0].strip() == msg.encode(encoding) assert l[0].strip() == msg.encode(encoding)
class BaseTests: class TestTerminalWriter:
def test_line(self): def pytest_generate_tests(self, metafunc):
tw = self.getwriter() if "tw" in metafunc.funcargnames:
metafunc.addcall(id="path", param="path")
metafunc.addcall(id="stringio", param="stringio")
metafunc.addcall(id="callable", param="callable")
def pytest_funcarg__tw(self, request):
if request.param == "path":
tmpdir = request.getfuncargvalue("tmpdir")
p = tmpdir.join("tmpfile")
tw = py.io.TerminalWriter(p.open('w+'))
def getlines():
tw._file.flush()
return p.open('r').readlines()
elif request.param == "stringio":
tw = py.io.TerminalWriter(stringio=True)
def getlines():
tw.stringio.seek(0)
return tw.stringio.readlines()
elif request.param == "callable":
writes = []
tw = py.io.TerminalWriter(writes.append)
def getlines():
io = py.io.TextIO()
io.write("".join(writes))
io.seek(0)
return io.readlines()
tw.getlines = getlines
return tw
def test_line(self, tw):
tw.line("hello") tw.line("hello")
l = self.getlines() l = tw.getlines()
assert len(l) == 1 assert len(l) == 1
assert l[0] == "hello\n" assert l[0] == "hello\n"
def test_line_unicode(self): def test_line_unicode(self, tw):
tw = self.getwriter()
for encoding in 'utf8', 'latin1': for encoding in 'utf8', 'latin1':
tw._encoding = encoding tw._encoding = encoding
msg = py.builtin._totext('b\u00f6y', 'utf8') msg = py.builtin._totext('b\u00f6y', 'utf8')
tw.line(msg) tw.line(msg)
l = self.getlines() l = tw.getlines()
assert l[0] == msg + "\n" assert l[0] == msg + "\n"
def test_sep_no_title(self): def test_sep_no_title(self, tw):
tw = self.getwriter()
tw.sep("-", fullwidth=60) tw.sep("-", fullwidth=60)
l = self.getlines() l = tw.getlines()
assert len(l) == 1 assert len(l) == 1
assert l[0] == "-" * 60 + "\n" assert l[0] == "-" * 60 + "\n"
def test_sep_with_title(self): def test_sep_with_title(self, tw):
tw = self.getwriter()
tw.sep("-", "hello", fullwidth=60) tw.sep("-", "hello", fullwidth=60)
l = self.getlines() l = tw.getlines()
assert len(l) == 1 assert len(l) == 1
assert l[0] == "-" * 26 + " hello " + "-" * 27 + "\n" assert l[0] == "-" * 26 + " hello " + "-" * 27 + "\n"
@py.test.mark.skipif("sys.platform == 'win32'") @py.test.mark.skipif("sys.platform == 'win32'")
def test__escaped(self): def test__escaped(self, tw):
tw = self.getwriter()
text2 = tw._escaped("hello", (31)) text2 = tw._escaped("hello", (31))
assert text2.find("hello") != -1 assert text2.find("hello") != -1
@py.test.mark.skipif("sys.platform == 'win32'") @py.test.mark.skipif("sys.platform == 'win32'")
def test_markup(self): def test_markup(self, tw):
tw = self.getwriter()
for bold in (True, False): for bold in (True, False):
for color in ("red", "green"): for color in ("red", "green"):
text2 = tw.markup("hello", **{color: True, 'bold': bold}) text2 = tw.markup("hello", **{color: True, 'bold': bold})
@ -91,53 +114,22 @@ class BaseTests:
py.test.raises(ValueError, "tw.markup('x', wronkw=3)") py.test.raises(ValueError, "tw.markup('x', wronkw=3)")
py.test.raises(ValueError, "tw.markup('x', wronkw=0)") py.test.raises(ValueError, "tw.markup('x', wronkw=0)")
def test_line_write_markup(self): def test_line_write_markup(self, tw):
tw = self.getwriter()
tw.hasmarkup = True tw.hasmarkup = True
tw.line("x", bold=True) tw.line("x", bold=True)
tw.write("x\n", red=True) tw.write("x\n", red=True)
l = self.getlines() l = tw.getlines()
if sys.platform != "win32": if sys.platform != "win32":
assert len(l[0]) > 2, l assert len(l[0]) > 2, l
assert len(l[1]) > 2, l assert len(l[1]) > 2, l
def test_attr_fullwidth(self): def test_attr_fullwidth(self, tw):
tw = self.getwriter()
tw.sep("-", "hello", fullwidth=70) tw.sep("-", "hello", fullwidth=70)
tw.fullwidth = 70 tw.fullwidth = 70
tw.sep("-", "hello") tw.sep("-", "hello")
l = self.getlines() l = tw.getlines()
assert len(l[0]) == len(l[1]) assert len(l[0]) == len(l[1])
class TestTmpfile(BaseTests):
def getwriter(self):
self.path = py.test.config.ensuretemp("terminalwriter").ensure("tmpfile")
self.tw = py.io.TerminalWriter(self.path.open('w+'))
return self.tw
def getlines(self):
io = self.tw._file
io.flush()
return self.path.open('r').readlines()
class TestWithStringIO(BaseTests):
def getwriter(self):
self.tw = py.io.TerminalWriter(stringio=True)
return self.tw
def getlines(self):
io = self.tw.stringio
io.seek(0)
return io.readlines()
class TestCallableFile(BaseTests):
def getwriter(self):
self.writes = []
return py.io.TerminalWriter(self.writes.append)
def getlines(self):
io = py.io.TextIO()
io.write("".join(self.writes))
io.seek(0)
return io.readlines()
def test_attr_hasmarkup(): def test_attr_hasmarkup():
tw = py.io.TerminalWriter(stringio=True) tw = py.io.TerminalWriter(stringio=True)
@ -146,6 +138,3 @@ def test_attr_hasmarkup():
tw.line("hello", bold=True) tw.line("hello", bold=True)
s = tw.stringio.getvalue() s = tw.stringio.getvalue()
assert len(s) > len("hello") assert len(s) > len("hello")

View File

@ -391,15 +391,15 @@ class TestGenfuncFunctional:
assert request._pyfuncitem._genid == "0" assert request._pyfuncitem._genid == "0"
return request.param return request.param
def test_function(metafunc): def test_function(metafunc, pytestconfig):
assert metafunc.config == py.test.config assert metafunc.config == pytestconfig
assert metafunc.module.__name__ == __name__ assert metafunc.module.__name__ == __name__
assert metafunc.function == test_function assert metafunc.function == test_function
assert metafunc.cls is None assert metafunc.cls is None
class TestClass: class TestClass:
def test_method(self, metafunc): def test_method(self, metafunc, pytestconfig):
assert metafunc.config == py.test.config assert metafunc.config == pytestconfig
assert metafunc.module.__name__ == __name__ assert metafunc.module.__name__ == __name__
if py.std.sys.version_info > (3, 0): if py.std.sys.version_info > (3, 0):
unbound = TestClass.test_method unbound = TestClass.test_method

View File

@ -74,8 +74,8 @@ class TestBootstrapping:
x500 = testdir.makepyfile(pytest_x500="#") x500 = testdir.makepyfile(pytest_x500="#")
p = testdir.makepyfile(""" p = testdir.makepyfile("""
import py import py
def test_hello(): def test_hello(pytestconfig):
plugin = py.test.config.pluginmanager.getplugin('x500') plugin = pytestconfig.pluginmanager.getplugin('x500')
assert plugin is not None assert plugin is not None
""") """)
monkeypatch.setenv('PYTEST_PLUGINS', 'pytest_x500', prepend=",") monkeypatch.setenv('PYTEST_PLUGINS', 'pytest_x500', prepend=",")