[py-svn] r61433 - py/branch/pytestplugin/py/test/plugin

hpk at codespeak.net hpk at codespeak.net
Wed Jan 28 21:06:59 CET 2009


Author: hpk
Date: Wed Jan 28 21:06:57 2009
New Revision: 61433

Modified:
   py/branch/pytestplugin/py/test/plugin/pytest_plugintester.py
   py/branch/pytestplugin/py/test/plugin/pytest_pytester.py
   py/branch/pytestplugin/py/test/plugin/pytest_resultlog.py
   py/branch/pytestplugin/py/test/plugin/pytest_terminal.py
Log:
getting close to not importing internal objects in the plugins 
(except pytest_plugintester and pytest_pytester whose purpose 
is to provide objects for testing plugins and test runs)


Modified: py/branch/pytestplugin/py/test/plugin/pytest_plugintester.py
==============================================================================
--- py/branch/pytestplugin/py/test/plugin/pytest_plugintester.py	(original)
+++ py/branch/pytestplugin/py/test/plugin/pytest_plugintester.py	Wed Jan 28 21:06:57 2009
@@ -6,10 +6,10 @@
 class Plugintester:
     def pytest_itemexecute_arg(self, pyfuncitem, argname):
         if argname == "plugintester":
-            suptest = PluginTester(pyfuncitem) 
+            pt = PluginTester(pyfuncitem) 
         else:
             return None
-        return suptest, suptest.finalize 
+        return pt, pt.finalize 
 
 class Support(object):
     def __init__(self, pyfuncitem):

Modified: py/branch/pytestplugin/py/test/plugin/pytest_pytester.py
==============================================================================
--- py/branch/pytestplugin/py/test/plugin/pytest_pytester.py	(original)
+++ py/branch/pytestplugin/py/test/plugin/pytest_pytester.py	Wed Jan 28 21:06:57 2009
@@ -91,20 +91,24 @@
     def runpytest(self, *args):
         return self.runpybin("py.test", *args)
 
-class Pytester:
-    def pytest_itemexecute_arg(self, pyfuncitem, argname):
-        if argname == "linecomp":
-            return LineComp(), None
-
 class LineComp:
-    def assert_contains_lines(self, lines1, lines2):
+
+    def __init__(self):
+        self.stringio = py.std.StringIO.StringIO()
+
+    def assert_contains_lines(self, lines1=None, lines2=None):
         """ assert that lines2 are contained (linearly) in lines1. 
             return a list of extralines found.
         """
         from fnmatch import fnmatch
         __tracebackhide__ = True
+        if lines2 is None:
+            lines2 = lines1
+            lines1 = self.stringio
         if hasattr(lines1, "getvalue"):
-            lines1 = lines1.getvalue().split("\n")
+            val = lines1.getvalue()
+            lines1.truncate(0)  # remove what we got 
+            lines1 = val.split("\n")
         if isinstance(lines2, str):
             lines2 = py.code.Source(lines2)
         if isinstance(lines2, py.code.Source):
@@ -136,3 +140,40 @@
                     raise AssertionError("expected line not found: %r" % line)
         extralines.extend(lines1)
         return extralines 
+
+class TSession:
+    def __init__(self, pyfuncitem):
+        self.pyfuncitem = pyfuncitem
+        self.tmpdir = py.test.ensuretemp("_".join(pyfuncitem.listnames()))
+        self.fstester = FSTester(self.tmpdir)
+        #self.capture = py.io.StdCapture()
+
+    #def finalize(self):
+    #    self.capture.reset()
+    #
+    def genitems(self, colitems):
+        return self.session.genitems(colitems)
+
+    def parseconfig(self, *args):
+        return py.test.config._reparse(list(args))
+
+    def getitem(self,  source, funcname="test_func"):
+        modcol = self.getmodulecol(source)
+        item = modcol.join(funcname) 
+        assert item is not None, "%r item not found in module:\n%s" %(funcname, source)
+        return item 
+
+    def getmodulecol(self,  source, configargs=(), withsession=False):
+        kw = {self.pyfuncitem.name: py.code.Source(source).strip()}
+        path = self.fstester.makepyfile(**kw)
+        self.config = self.parseconfig(path, *configargs)
+        self.session = self.config.initsession()
+        return self.config.getfsnode(path)
+
+class Pytester:
+    def pytest_itemexecute_arg(self, pyfuncitem, argname):
+        if argname == "linecomp":
+            return LineComp(), None
+        elif argname == "tsession":
+            tsession = TSession(pyfuncitem)
+            return tsession, None

Modified: py/branch/pytestplugin/py/test/plugin/pytest_resultlog.py
==============================================================================
--- py/branch/pytestplugin/py/test/plugin/pytest_resultlog.py	(original)
+++ py/branch/pytestplugin/py/test/plugin/pytest_resultlog.py	Wed Jan 28 21:06:57 2009
@@ -99,7 +99,6 @@
 # ===============================================================================
 
 import os, StringIO
-from py.__.test.testing import suptest
 
 def test_generic_path():
     from py.__.test.collect import Node, Item, FSCollector
@@ -161,31 +160,33 @@
     assert entry_lines[1:] == [' '+line for line in longrepr.splitlines()]
 
     
-class TestWithFunctionIntegration(suptest.InlineSession):
+class TestWithFunctionIntegration:
     # XXX (hpk) i think that the resultlog plugin should
     # provide a Parser object so that one can remain 
     # ignorant regarding formatting details.  
-    def getresultlog(self, args):
-        resultlog = self.tmpdir.join("resultlog")
-        self.parse_and_run("--resultlog=%s" % resultlog, args)
+    def getresultlog(self, fstester, arg):
+        resultlog = fstester.tmpdir.join("resultlog")
+        args = ["--resultlog=%s" % resultlog] + [arg]
+        fstester.runpytest(*args)
         return filter(None, resultlog.readlines(cr=0))
         
-    def test_collection_report(self):
-        ok = self.makepyfile(test_collection_ok="")
-        skip = self.makepyfile(test_collection_skip="import py ; py.test.skip('hello')")
-        fail = self.makepyfile(test_collection_fail="XXX")
+    def test_collection_report(self, plugintester):
+        fstester = plugintester.fstester()
+        ok = fstester.makepyfile(test_collection_ok="")
+        skip = fstester.makepyfile(test_collection_skip="import py ; py.test.skip('hello')")
+        fail = fstester.makepyfile(test_collection_fail="XXX")
 
-        lines = self.getresultlog(ok) 
+        lines = self.getresultlog(fstester, ok) 
         assert not lines
 
-        lines = self.getresultlog(skip)
+        lines = self.getresultlog(fstester, skip)
         assert len(lines) == 2
         assert lines[0].startswith("S ")
         assert lines[0].endswith("test_collection_skip.py")
         assert lines[1].startswith(" ")
         assert lines[1].endswith("test_collection_skip.py:1: Skipped: 'hello'")
 
-        lines = self.getresultlog(fail)
+        lines = self.getresultlog(fstester, fail)
         assert lines
         assert lines[0].startswith("F ")
         assert lines[0].endswith("test_collection_fail.py"), lines[0]
@@ -193,14 +194,15 @@
             assert x.startswith(" ")
         assert "XXX" in "".join(lines[1:])
 
-    def test_log_test_outcomes(self):
-        mod = self.makepyfile(test_mod="""
+    def test_log_test_outcomes(self, plugintester):
+        fstester = plugintester.fstester()
+        mod = fstester.makepyfile(test_mod="""
             import py 
             def test_pass(): pass
             def test_skip(): py.test.skip("hello")
             def test_fail(): raise ValueError("val")
         """)
-        lines = self.getresultlog(mod)
+        lines = self.getresultlog(fstester, mod)
         assert len(lines) >= 3
         assert lines[0].startswith(". ")
         assert lines[0].endswith("test_pass")

Modified: py/branch/pytestplugin/py/test/plugin/pytest_terminal.py
==============================================================================
--- py/branch/pytestplugin/py/test/plugin/pytest_terminal.py	(original)
+++ py/branch/pytestplugin/py/test/plugin/pytest_terminal.py	Wed Jan 28 21:06:57 2009
@@ -344,30 +344,21 @@
 
 from py.__.test import event
 from py.__.test.runner import basic_run_report
-from py.__.test.testing import suptest 
-from py.__.test.testing.suptest import popvalue, assert_stringio_contains_lines
 from py.__.test.dsession.hostmanage import Host, makehostup
 
-class TestTerminal(suptest.InlineCollection):
-    #def test_reporter_subscription_by_default(self):
-    #    config = py.test.config._reparse(['xxx'])
-    #    plugin = config.pluginmanager.getplugin("pytest_terminal")
-    #    assert plugin
-
-    def test_hostup(self):
-        item = self.getitem("def test_func(): pass")
-        stringio = py.std.cStringIO.StringIO()
-        rep = TerminalReporter(item._config, file=stringio)
-        rep.processevent(event.TestrunStart())
+class TestTerminal:
+    def test_hostup(self, tsession, linecomp):
+        item = tsession.getitem("def test_func(): pass")
+        rep = TerminalReporter(item._config, linecomp.stringio)
         host = Host("localhost")
         rep.processevent(makehostup(host))
-        s = popvalue(stringio)
-        expect = "%s %s %s - Python %s" %(host.hostid, sys.platform, 
+        linecomp.assert_contains_lines([
+            "*%s %s %s - Python %s" %(host.hostid, sys.platform, 
             sys.executable, repr_pythonversion(sys.version_info))
-        assert s.find(expect) != -1
+        ])
 
-    def test_pass_skip_fail(self, linecomp):
-        modcol = self.getmodulecol("""
+    def test_pass_skip_fail(self, tsession, linecomp):
+        modcol = tsession.getmodulecol("""
             import py
             def test_ok():
                 pass
@@ -375,24 +366,25 @@
                 py.test.skip("xx")
             def test_func():
                 assert 0
-        """, withsession=True)
-        stringio = py.std.cStringIO.StringIO()
-        rep = TerminalReporter(modcol._config, file=stringio)
+        """)
+        rep = TerminalReporter(modcol._config, file=linecomp.stringio)
         rep.processevent(event.TestrunStart())
-        for item in self.session.genitems([modcol]):
+        for item in tsession.genitems([modcol]):
             ev = basic_run_report(item) 
             rep.processevent(ev)
-        s = popvalue(stringio)
-        assert s.find("test_pass_skip_fail.py .sF") != -1
+        linecomp.assert_contains_lines([
+                "*test_pass_skip_fail.py .sF"
+        ])
+
         rep.processevent(event.TestrunFinish())
-        linecomp.assert_contains_lines(stringio, [
+        linecomp.assert_contains_lines([
             "    def test_func():",
             ">       assert 0",
             "E       assert 0",
         ])
 
-    def test_pass_skip_fail_verbose(self):
-        modcol = self.getmodulecol("""
+    def test_pass_skip_fail_verbose(self, tsession, linecomp):
+        modcol = tsession.getmodulecol("""
             import py
             def test_ok():
                 pass
@@ -400,122 +392,116 @@
                 py.test.skip("xx")
             def test_func():
                 assert 0
-        """, configargs=("-v",), withsession=True)
-        stringio = py.std.cStringIO.StringIO()
-        rep = TerminalReporter(modcol._config, file=stringio)
+        """, configargs=("-v",))
+        rep = TerminalReporter(modcol._config, file=linecomp.stringio)
         rep.processevent(event.TestrunStart())
         items = modcol.collect()
         for item in items:
             rep.processevent(event.ItemStart(item))
-            s = stringio.getvalue().strip()
+            s = linecomp.stringio.getvalue().strip()
             assert s.endswith(item.name)
             ev = basic_run_report(item) 
             rep.processevent(ev)
 
-        assert_stringio_contains_lines(stringio, [
+        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.processevent(event.TestrunFinish())
-        assert_stringio_contains_lines(stringio, [
+        linecomp.assert_contains_lines([
             "    def test_func():",
             ">       assert 0",
             "E       assert 0",
         ])
 
-    def test_collect_fail(self):
-        modcol = self.getmodulecol("""
-            import xyz
-        """, withsession=True)
-        stringio = py.std.cStringIO.StringIO()
-        rep = TerminalReporter(modcol._config, file=stringio)
-        self.session.bus.subscribe(rep.processevent) 
+    def test_collect_fail(self, tsession, linecomp):
+        modcol = tsession.getmodulecol("import xyz")
+        rep = TerminalReporter(modcol._config, file=linecomp.stringio)
+        tsession.session.bus.subscribe(rep.processevent) 
         rep.processevent(event.TestrunStart())
-        l = list(self.session.genitems([modcol]))
+        l = list(tsession.genitems([modcol]))
         assert len(l) == 0
-        s = popvalue(stringio) 
-        print s
-        assert s.find("test_collect_fail.py F") != -1
+        linecomp.assert_contains_lines([
+            "*test_collect_fail.py F*"
+        ])
         rep.processevent(event.TestrunFinish())
-        assert_stringio_contains_lines(stringio, [
+        linecomp.assert_contains_lines([
             ">   import xyz",
             "E   ImportError: No module named xyz"
         ])
 
-    def test_internal_exception(self):
-        modcol = self.getmodulecol("def test_one(): pass")
-        stringio = py.std.cStringIO.StringIO()
-        rep = TerminalReporter(modcol._config, file=stringio)
+    def test_internal_exception(self, tsession, linecomp):
+        modcol = tsession.getmodulecol("def test_one(): pass")
+        rep = TerminalReporter(modcol._config, file=linecomp.stringio)
         excinfo = py.test.raises(ValueError, "raise ValueError('hello')")
         rep.processevent(event.InternalException(excinfo))
-        s = popvalue(stringio)
-        assert s.find("InternalException:") != -1 
+        linecomp.assert_contains_lines([
+            "InternalException: >*raise ValueError*"
+        ])
 
-    def test_hostready_crash(self):
-        modcol = self.getmodulecol("""
+    def test_hostready_crash(self, tsession, linecomp):
+        modcol = tsession.getmodulecol("""
             def test_one():
                 pass
         """, configargs=("-v",))
-        stringio = py.std.cStringIO.StringIO()
         host1 = Host("localhost")
-        rep = TerminalReporter(modcol._config, file=stringio)
+        rep = TerminalReporter(modcol._config, file=linecomp.stringio)
         rep.processevent(event.HostGatewayReady(host1, None))
-        s = popvalue(stringio)
-        assert s.find("HostGatewayReady") != -1
+        linecomp.assert_contains_lines([
+            "*HostGatewayReady*"
+        ])
         rep.processevent(event.HostDown(host1, "myerror"))
-        s = popvalue(stringio)
-        assert s.find("HostDown") != -1
-        assert s.find("myerror") != -1
+        linecomp.assert_contains_lines([
+            "*HostDown*myerror*", 
+        ])
 
-    def test_writeline(self):
-        modcol = self.getmodulecol("def test_one(): pass")
+    def test_writeline(self, tsession, linecomp):
+        modcol = tsession.getmodulecol("def test_one(): pass")
         stringio = py.std.cStringIO.StringIO()
-        rep = TerminalReporter(modcol._config, file=stringio)
+        rep = TerminalReporter(modcol._config, file=linecomp.stringio)
         rep.write_fspath_result(py.path.local("xy.py"), '.')
         rep.write_line("hello world")
-        lines = popvalue(stringio).split('\n')
+        lines = linecomp.stringio.getvalue().split('\n')
         assert not lines[0]
         assert lines[1].endswith("xy.py .")
         assert lines[2] == "hello world"
 
-    def test_looponfailingreport(self):
-        modcol = self.getmodulecol("""
+    def test_looponfailingreport(self, tsession, linecomp):
+        modcol = tsession.getmodulecol("""
             def test_fail():
                 assert 0
             def test_fail2():
                 raise ValueError()
         """)
-        stringio = py.std.cStringIO.StringIO()
-        rep = TerminalReporter(modcol._config, file=stringio)
+        rep = TerminalReporter(modcol._config, file=linecomp.stringio)
         reports = [basic_run_report(x) for x in modcol.collect()]
         rep.processevent(event.LooponfailingInfo(reports, [modcol._config.topdir]))
-        assert_stringio_contains_lines(stringio, [
+        linecomp.assert_contains_lines([
             "*test_looponfailingreport.py:2: assert 0",
             "*test_looponfailingreport.py:4: ValueError*",
             "*waiting*", 
             "*%s*" % (modcol._config.topdir),
         ])
 
-    def test_tb_option(self):
+    def test_tb_option(self, tsession, linecomp):
         for tbopt in ["no", "short", "long"]:
             print 'testing --tb=%s...' % tbopt
-            modcol = self.getmodulecol("""
+            modcol = tsession.getmodulecol("""
                 import py
                 def g():
                     raise IndexError
                 def test_func():
                     print 6*7
                     g()  # --calling--
-            """, configargs=("--tb=%s" % tbopt,), withsession=True)
-            stringio = py.std.cStringIO.StringIO()
-            rep = TerminalReporter(modcol._config, file=stringio)
+            """, configargs=("--tb=%s" % tbopt,))
+            rep = TerminalReporter(modcol._config, file=linecomp.stringio)
             rep.processevent(event.TestrunStart())
-            for item in self.session.genitems([modcol]):
+            for item in tsession.genitems([modcol]):
                 ev = basic_run_report(item) 
                 rep.processevent(ev)
             rep.processevent(event.TestrunFinish())
-            s = popvalue(stringio)
+            s = linecomp.stringio.getvalue()
             if tbopt == "long":
                 assert 'print 6*7' in s
             else:
@@ -527,66 +513,64 @@
                 assert 'FAILURES' not in s
                 assert '--calling--' not in s
                 assert 'IndexError' not in s
+            linecomp.stringio.truncate(0)
 
-    def test_show_path_before_running_test(self):
-        modcol = self.getmodulecol("""
+    def test_show_path_before_running_test(self, tsession, linecomp):
+        modcol = tsession.getmodulecol("""
             def test_foobar():
                 pass
-        """, withsession=True)
-        stringio = py.std.cStringIO.StringIO()
-        rep = TerminalReporter(modcol._config, file=stringio)
-        l = list(self.session.genitems([modcol]))
+        """)
+        rep = TerminalReporter(modcol._config, file=linecomp.stringio)
+        l = list(tsession.genitems([modcol]))
         assert len(l) == 1
         rep.processevent(event.ItemStart(l[0]))
-        s = popvalue(stringio) 
-        print s
-        assert s.find("test_show_path_before_running_test.py") != -1
+        linecomp.assert_contains_lines([
+            "*test_show_path_before_running_test.py*"
+        ])
 
-    def pseudo_keyboard_interrupt(self, verbose=False):
-        modcol = self.getmodulecol("""
+    def pseudo_keyboard_interrupt(self, tsession, linecomp, verbose=False):
+        modcol = tsession.getmodulecol("""
             def test_foobar():
                 assert 0
             def test_spamegg():
                 import py; py.test.skip('skip me please!')
             def test_interrupt_me():
                 raise KeyboardInterrupt   # simulating the user
-        """, configargs=("--showskipsummary",) + ("-v",)*verbose,
-             withsession=True)
-        stringio = py.std.cStringIO.StringIO()
-        rep = TerminalReporter(modcol._config, file=stringio)
+        """, configargs=("--showskipsummary",) + ("-v",)*verbose)
+        rep = TerminalReporter(modcol._config, file=linecomp.stringio)
         rep.processevent(event.TestrunStart())
         try:
-            for item in self.session.genitems([modcol]):
+            for item in tsession.genitems([modcol]):
                 ev = basic_run_report(item) 
                 rep.processevent(ev)
         except KeyboardInterrupt:
             excinfo = py.code.ExceptionInfo()
         else:
             py.test.fail("no KeyboardInterrupt??")
-        s = popvalue(stringio)
+        s = linecomp.stringio.getvalue()
         if not verbose:
             assert s.find("_keyboard_interrupt.py Fs") != -1
         rep.processevent(event.TestrunFinish(exitstatus=2, excinfo=excinfo))
-        assert_stringio_contains_lines(stringio, [
+        text = linecomp.stringio.getvalue()
+        linecomp.assert_contains_lines([
             "    def test_foobar():",
             ">       assert 0",
             "E       assert 0",
         ])
-        text = stringio.getvalue()
         assert "Skipped: 'skip me please!'" in text
         assert "_keyboard_interrupt.py:6: KeyboardInterrupt" in text
         see_details = "raise KeyboardInterrupt   # simulating the user" in text
         assert see_details == verbose
 
-    def test_keyboard_interrupt(self):
-        self.pseudo_keyboard_interrupt()
+    def test_keyboard_interrupt(self, tsession, linecomp):
+        self.pseudo_keyboard_interrupt(tsession, linecomp)
         
-    def test_verbose_keyboard_interrupt(self):
-        self.pseudo_keyboard_interrupt(verbose=True)
+    def test_verbose_keyboard_interrupt(self, tsession, linecomp):
+        self.pseudo_keyboard_interrupt(tsession, linecomp, verbose=True)
 
-class TestCollectonly(suptest.InlineCollection):
-    def test_collectonly_basic(self):
-        modcol = self.getmodulecol(configargs=['--collectonly'], source="""
+class TestCollectonly:
+    def test_collectonly_basic(self, tsession, linecomp):
+        modcol = tsession.getmodulecol(configargs=['--collectonly'], source="""
             def test_func():
                 pass
         """)
@@ -594,42 +578,43 @@
         rep = CollectonlyReporter(modcol._config, out=stringio)
         indent = rep.indent
         rep.processevent(event.CollectionStart(modcol))
-        s = popvalue(stringio)
-        assert s == "<Module 'test_TestCollectonly_test_collectonly_basic.py'>"
-
+        linecomp.assert_contains_lines(stringio, [
+           "<Module 'test_collectonly_basic.py'>"
+        ])
         item = modcol.join("test_func")
         rep.processevent(event.ItemStart(item))
-        s = popvalue(stringio)
-        assert s.find("Function 'test_func'") != -1
+        linecomp.assert_contains_lines(stringio, [
+           "  <Function 'test_func'>", 
+        ])
         rep.processevent(event.CollectionReport(modcol, [], excinfo=None))
         assert rep.indent == indent 
 
-    def test_collectonly_skipped_module(self):
-        modcol = self.getmodulecol(configargs=['--collectonly'], source="""
+    def test_collectonly_skipped_module(self, tsession, linecomp):
+        modcol = tsession.getmodulecol(configargs=['--collectonly'], source="""
             import py
             py.test.skip("nomod")
-        """, withsession=True)
+        """)
         stringio = py.std.cStringIO.StringIO()
         rep = CollectonlyReporter(modcol._config, out=stringio)
-        self.session.bus.subscribe(rep.processevent) 
-        cols = list(self.session.genitems([modcol]))
+        tsession.session.bus.subscribe(rep.processevent) 
+        cols = list(tsession.genitems([modcol]))
         assert len(cols) == 0
-        assert_stringio_contains_lines(stringio, """
-            <Module 'test_TestCollectonly_test_collectonly_skipped_module.py'>
+        linecomp.assert_contains_lines(stringio, """
+            <Module 'test_collectonly_skipped_module.py'>
               !!! Skipped: 'nomod' !!!
         """)
 
-    def test_collectonly_failed_module(self):
-        modcol = self.getmodulecol(configargs=['--collectonly'], source="""
+    def test_collectonly_failed_module(self, tsession, linecomp):
+        modcol = tsession.getmodulecol(configargs=['--collectonly'], source="""
             raise ValueError(0)
-        """, withsession=True)
+        """)
         stringio = py.std.cStringIO.StringIO()
         rep = CollectonlyReporter(modcol._config, out=stringio)
-        self.session.bus.subscribe(rep.processevent) 
-        cols = list(self.session.genitems([modcol]))
+        tsession.session.bus.subscribe(rep.processevent) 
+        cols = list(tsession.genitems([modcol]))
         assert len(cols) == 0
-        assert_stringio_contains_lines(stringio, """
-            <Module 'test_TestCollectonly_test_collectonly_failed_module.py'>
+        linecomp.assert_contains_lines(stringio, """
+            <Module 'test_collectonly_failed_module.py'>
               !!! ValueError: 0 !!!
         """)
 



More information about the pytest-commit mailing list