[py-svn] r61974 - in py/branch/pytestplugin/py/test: . plugin testing

hpk at codespeak.net hpk at codespeak.net
Tue Feb 17 17:55:21 CET 2009


Author: hpk
Date: Tue Feb 17 17:55:18 2009
New Revision: 61974

Modified:
   py/branch/pytestplugin/py/test/config.py
   py/branch/pytestplugin/py/test/event.py
   py/branch/pytestplugin/py/test/handleplugin.py
   py/branch/pytestplugin/py/test/plugin/pytest_terminal.py
   py/branch/pytestplugin/py/test/testing/test_event.py
Log:
refactoring terminal reporter towards keyword based event handling. 
tests pass, so i commit. 



Modified: py/branch/pytestplugin/py/test/config.py
==============================================================================
--- py/branch/pytestplugin/py/test/config.py	(original)
+++ py/branch/pytestplugin/py/test/config.py	Tue Feb 17 17:55:18 2009
@@ -4,7 +4,6 @@
 from conftesthandle import Conftest
 from py.__.test.defaultconftest import adddefaultoptions
 from py.__.test.handleplugin import PytestPluginManager
-from py.__.test.event import EventBus
 
 optparse = py.compat.optparse
 
@@ -36,7 +35,7 @@
         self._parser = optparse.OptionParser(
             usage="usage: %prog [options] [query] [filenames of tests]")
         if bus is None:
-            bus = EventBus()
+            bus = py.test._EventBus()
         self.bus = bus
         if pluginmanager is None:
             pluginmanager = PytestPluginManager()
@@ -262,7 +261,7 @@
     
 # this is the one per-process instance of py.test configuration 
 config_per_process = Config(
-    bus=EventBus(bus=py.event), 
+    bus=py.test._EventBus(bus=py.event), 
     pluginmanager=PytestPluginManager(py.plugin))
 
 # default import paths for sessions 

Modified: py/branch/pytestplugin/py/test/event.py
==============================================================================
--- py/branch/pytestplugin/py/test/event.py	(original)
+++ py/branch/pytestplugin/py/test/event.py	Tue Feb 17 17:55:18 2009
@@ -21,9 +21,13 @@
             bus = py.__.misc.event.EventBus()
         self._bus = bus
 
-    def issubscribed(self, callback):
-        for x in self._bus._getsubscribers(''):
-            if isinstance(x, CallWithValue) and x.callback == callback:
+    def issubscribed(self, callback=None, **kw):
+        if callback is not None:
+            for x in self._bus._getsubscribers(''):
+                if isinstance(x, CallWithValue) and x.callback == callback:
+                    return True
+        for name, value in kw.items():
+            if value in self._bus._getsubscribers(name):
                 return True
     
     def subscribe(self, callback=None, **kw):
@@ -32,6 +36,13 @@
             callback = CallWithValue(callback)
         self._bus.subscribe(callback, **kw)
 
+    def subscribe_methods(self, instance, prefix = "pyevent_"):
+        for cls in py.std.inspect.getmro(instance.__class__):
+            for attrname in cls.__dict__:
+                if attrname.startswith(prefix):
+                    name = attrname[len(prefix):]
+                    self.subscribe(**{name: getattr(instance, attrname)})
+
     def unsubscribe(self, callback, **kw):
         """ unsubscribe given callback from bus events. """ 
         for x in self._bus._getsubscribers(''):

Modified: py/branch/pytestplugin/py/test/handleplugin.py
==============================================================================
--- py/branch/pytestplugin/py/test/handleplugin.py	(original)
+++ py/branch/pytestplugin/py/test/handleplugin.py	Tue Feb 17 17:55:18 2009
@@ -41,7 +41,7 @@
             clsname = spec.__name__
             plugin = spec()
             self.pm.register(plugin)
-        print "registered", plugin, "at", self
+        #print "registered", plugin, "at", self
 
     # 
     #

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	Tue Feb 17 17:55:18 2009
@@ -32,6 +32,74 @@
         else:
             self.reporter.processevent(obj)
 
+class OutcomeStats:
+    def __init__(self):
+        self._reset()
+
+    def _reset(self):
+        self.skipped = []
+        self.passed = []
+        self.failed = []
+        self.deselected = []
+
+    def pyevent_itemtestreport(self, ev):
+        for name in 'skipped failed passed'.split():
+            if getattr(ev, name):
+                getattr(self, name).append(ev)
+
+    def pyevent_collectionreport(self, ev):
+        for name in 'skipped failed'.split():
+            if getattr(ev, name):
+                getattr(self, name).append(ev)
+                return 
+
+    def pyevent_deselected(self, ev):
+        self.deselected.extend(ev.items)
+
+    def pyevent_testrunstart(self, ev):
+        self._reset()
+
+def test_outcomestats():
+    names = 'passed skipped deselected failed'.split()
+    #for name in names:
+    #    stats = OutcomeStats()
+    #    assert getattr(stats, name) == []
+    #    bus = py.test._EventBus()
+    #    bus.subscribe_methods(stats)
+    #    class X: pass
+    #    for name2 in names:
+    #        if name2 != name:
+    #            setattr(X, name2, False)
+    #        else:
+    #            setattr(X, name2, True)
+    #    bus.notify(itemtestreport=X)
+    #    assert getattr(stats, name) == [X]
+
+    stats = OutcomeStats()
+    bus = py.test._EventBus()
+    bus.subscribe_methods(stats)
+    class P:
+        passed = True
+        failed = skipped = False
+    bus.notify(itemtestreport=P)
+    assert stats.passed
+    assert stats.passed == [P]
+    bus.notify(itemtestreport=P)
+    assert stats.passed == [P, P]
+    class F:
+        skipped = passed = False 
+        failed = True
+    bus.notify(itemtestreport=F)
+    assert stats.failed == [F]
+    bus.notify(collectionreport=F)
+    assert stats.failed == [F, F]
+    class D:
+        items = [42]
+    bus.notify(deselected=D)
+    assert stats.deselected == [42]
+    
+    
+
 class BaseReporter(object):
     def __init__(self):
         self._reset()

Modified: py/branch/pytestplugin/py/test/testing/test_event.py
==============================================================================
--- py/branch/pytestplugin/py/test/testing/test_event.py	(original)
+++ py/branch/pytestplugin/py/test/testing/test_event.py	Tue Feb 17 17:55:18 2009
@@ -35,6 +35,15 @@
         bus.notify(x=22)
         assert l == [1]
 
+    def test_subscribe_methods(self):
+        bus = EventBus()
+        class A:
+            def pyevent_hello(self, ev): pass 
+        class B(A): pass
+        b = B()
+        bus.subscribe_methods(b)
+        assert bus.issubscribed(hello=b.pyevent_hello)
+
 def test_event_attributes():
     for name, value in vars(event).items():
         if py.std.inspect.isclass(value) and issubclass(value, event.BaseEvent):



More information about the pytest-commit mailing list