From 030986dcc468de8b5a33ae276744791ad3cb1a40 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Wed, 13 Jan 2010 18:04:58 +0100 Subject: [PATCH] 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 --- ISSUES.txt | 19 ++++-- testing/acceptance_test.py | 4 +- testing/io_/test_terminalwriter.py | 99 +++++++++++++----------------- testing/test_funcargs.py | 8 +-- testing/test_pluginmanager.py | 4 +- 5 files changed, 66 insertions(+), 68 deletions(-) diff --git a/ISSUES.txt b/ISSUES.txt index b1db4ead0..269dc386a 100644 --- a/ISSUES.txt +++ b/ISSUES.txt @@ -51,14 +51,23 @@ but a remote one fail because the tests directory does not contain an "__init__.py". Either give 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 -The remaining uses of py.test.ensuretemp within the py-test base -itself are for setup methods. Also users have expressed the -wish to have funcargs available to setup functions. Experiment -with allowing funcargs there and finalizing deprecating py.test.ensuretemp. +consider deprecating py.test.ensuretemp and py.test.config +to further reduce py.test globality. Also consider +having py.test.config and ensuretemp coming from +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 --------------------------------------- diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index b6e31c30e..e9a7584aa 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -19,8 +19,8 @@ class TestGeneralUsage: """) testdir.makepyfile(test_one=""" import py - def test_option(): - assert py.test.config.option.xyz == "123" + def test_option(pytestconfig): + assert pytestconfig.option.xyz == "123" """) result = testdir.runpytest("-p", "xyz", "--xyz=123") assert result.ret == 0 diff --git a/testing/io_/test_terminalwriter.py b/testing/io_/test_terminalwriter.py index 644c91186..f5d4b0a8d 100644 --- a/testing/io_/test_terminalwriter.py +++ b/testing/io_/test_terminalwriter.py @@ -44,46 +44,69 @@ def test_unicode_encoding(): tw.line(msg) assert l[0].strip() == msg.encode(encoding) -class BaseTests: - def test_line(self): - tw = self.getwriter() +class TestTerminalWriter: + def pytest_generate_tests(self, metafunc): + 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") - l = self.getlines() + l = tw.getlines() assert len(l) == 1 assert l[0] == "hello\n" - def test_line_unicode(self): - tw = self.getwriter() + def test_line_unicode(self, tw): for encoding in 'utf8', 'latin1': tw._encoding = encoding msg = py.builtin._totext('b\u00f6y', 'utf8') tw.line(msg) - l = self.getlines() + l = tw.getlines() assert l[0] == msg + "\n" - def test_sep_no_title(self): - tw = self.getwriter() + def test_sep_no_title(self, tw): tw.sep("-", fullwidth=60) - l = self.getlines() + l = tw.getlines() assert len(l) == 1 assert l[0] == "-" * 60 + "\n" - def test_sep_with_title(self): - tw = self.getwriter() + def test_sep_with_title(self, tw): tw.sep("-", "hello", fullwidth=60) - l = self.getlines() + l = tw.getlines() assert len(l) == 1 assert l[0] == "-" * 26 + " hello " + "-" * 27 + "\n" @py.test.mark.skipif("sys.platform == 'win32'") - def test__escaped(self): - tw = self.getwriter() + def test__escaped(self, tw): text2 = tw._escaped("hello", (31)) assert text2.find("hello") != -1 @py.test.mark.skipif("sys.platform == 'win32'") - def test_markup(self): - tw = self.getwriter() + def test_markup(self, tw): for bold in (True, False): for color in ("red", "green"): 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=0)") - def test_line_write_markup(self): - tw = self.getwriter() + def test_line_write_markup(self, tw): tw.hasmarkup = True tw.line("x", bold=True) tw.write("x\n", red=True) - l = self.getlines() + l = tw.getlines() if sys.platform != "win32": assert len(l[0]) > 2, l assert len(l[1]) > 2, l - def test_attr_fullwidth(self): - tw = self.getwriter() + def test_attr_fullwidth(self, tw): tw.sep("-", "hello", fullwidth=70) tw.fullwidth = 70 tw.sep("-", "hello") - l = self.getlines() + l = tw.getlines() 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(): tw = py.io.TerminalWriter(stringio=True) @@ -146,6 +138,3 @@ def test_attr_hasmarkup(): tw.line("hello", bold=True) s = tw.stringio.getvalue() assert len(s) > len("hello") - - - diff --git a/testing/test_funcargs.py b/testing/test_funcargs.py index 7b9d61185..5c48a34b8 100644 --- a/testing/test_funcargs.py +++ b/testing/test_funcargs.py @@ -391,15 +391,15 @@ class TestGenfuncFunctional: assert request._pyfuncitem._genid == "0" return request.param - def test_function(metafunc): - assert metafunc.config == py.test.config + def test_function(metafunc, pytestconfig): + assert metafunc.config == pytestconfig assert metafunc.module.__name__ == __name__ assert metafunc.function == test_function assert metafunc.cls is None class TestClass: - def test_method(self, metafunc): - assert metafunc.config == py.test.config + def test_method(self, metafunc, pytestconfig): + assert metafunc.config == pytestconfig assert metafunc.module.__name__ == __name__ if py.std.sys.version_info > (3, 0): unbound = TestClass.test_method diff --git a/testing/test_pluginmanager.py b/testing/test_pluginmanager.py index 36ce12b47..ee13786d2 100644 --- a/testing/test_pluginmanager.py +++ b/testing/test_pluginmanager.py @@ -74,8 +74,8 @@ class TestBootstrapping: x500 = testdir.makepyfile(pytest_x500="#") p = testdir.makepyfile(""" import py - def test_hello(): - plugin = py.test.config.pluginmanager.getplugin('x500') + def test_hello(pytestconfig): + plugin = pytestconfig.pluginmanager.getplugin('x500') assert plugin is not None """) monkeypatch.setenv('PYTEST_PLUGINS', 'pytest_x500', prepend=",")