[py-svn] r57277 - in py/branch/event/py/test2: . testing

hpk at codespeak.net hpk at codespeak.net
Fri Aug 15 14:44:35 CEST 2008


Author: hpk
Date: Fri Aug 15 14:44:33 2008
New Revision: 57277

Modified:
   py/branch/event/py/test2/pycollect.py
   py/branch/event/py/test2/testing/test_setup_nested.py
Log:
always use fresh instances for test methods 


Modified: py/branch/event/py/test2/pycollect.py
==============================================================================
--- py/branch/event/py/test2/pycollect.py	(original)
+++ py/branch/event/py/test2/pycollect.py	Fri Aug 15 14:44:33 2008
@@ -226,6 +226,9 @@
         return []
     Function = property(Function)
 
+    def newinstance(self):  
+        self.obj = self._getobj()
+        return self.obj
 
 class FunctionMixin(PyobjMixin):
     """ mixin for the code common to Function and Generator.
@@ -239,7 +242,11 @@
             name = 'setup_method' 
         else: 
             name = 'setup_function' 
-        obj = self.parent.obj 
+        if isinstance(self.parent, Instance):
+            obj = self.parent.newinstance()
+            self.obj = self._getobj()
+        else:
+            obj = self.parent.obj 
         setup_func_or_method = getattr(obj, name, None)
         if setup_func_or_method is not None: 
             return setup_func_or_method(self.obj) 
@@ -253,7 +260,7 @@
         obj = self.parent.obj 
         teardown_func_or_meth = getattr(obj, name, None)
         if teardown_func_or_meth is not None: 
-            return teardown_func_or_meth(self.obj) 
+            teardown_func_or_meth(self.obj) 
 
     def _prunetraceback(self, traceback):
         if not self._config.option.fulltrace: 

Modified: py/branch/event/py/test2/testing/test_setup_nested.py
==============================================================================
--- py/branch/event/py/test2/testing/test_setup_nested.py	(original)
+++ py/branch/event/py/test2/testing/test_setup_nested.py	Fri Aug 15 14:44:33 2008
@@ -124,3 +124,20 @@
     rep = sorter.getreport("test_one") 
     assert rep.passed 
         
+def test_method_setup_uses_fresh_instances():
+    sorter = suptest.events_from_runsource("""
+        class TestSelfState1: 
+            def __init__(self):
+                self.hello = 42
+            def test_hello(self):
+                self.world = 23
+            def test_afterhello(self):
+                assert not hasattr(self, 'world')
+                assert self.hello == 42
+        class TestSelfState2: 
+            def test_hello(self):
+                self.world = 10
+            def test_world(self):
+                assert not hasattr(self, 'world')
+    """)
+    sorter.assertoutcome(passed=4, failed=0)



More information about the pytest-commit mailing list