What's the matter with this code section?

bruno modulix onurb at xiludom.gro
Wed Aug 24 08:45:13 EDT 2005


Johnny Lee wrote:
> Here is the source:
> 
(snip)
> class TestCaseTest(TestCase):
> 	def testRunning(self):
> 		print "testRunning in TestCaseTest"
> 		test = WasRun("testMethod")
> 		assert(not test.wasRun)
> 		test.run()
> 		assert(test.wasRun)
> 	def testSetUp(self):
> 		print "testSetUp in TestCaseTest"
> 		test = WasRun("testMethod")
> 		test.run()
Shouldn't it be test.setUp() instead ?

Unless the problem is here:

### TestCase.run() calls self.setUp()
class TestCase:
        (snip)
	def run(self):
		print "run in TestCase"
		self.setUp()
		method = getattr(self, self.name)
		method()

### WasRun.run() doesn't call self.setUp()
class WasRun(TestCase):
        (snip)
	def run(self):
		print "run in WasRun"
		method = getattr(self, self.name)
		method()
	def setUp(self):
		print "in setUp of WasRun"
		self.wasSetUp = 1


BTW, if the only reason for overloading WasRun.run() is to print "run in
WasRun", you can also modify parent's class run() method so it display
the effective class name, not an hard-coded one:

class TestCase:
        (snip)
	def run(self):
		print "run in %s" % self.__class__.__name__
		self.setUp()
		method = getattr(self, self.name)
		method()

and then remove run() from WasRun.

There should be a simple solution to 'aspect' the debug/trace stuff with
a decorator, like:

def traced(func):
  def _wrapper(self, *args, **kwargs):
    print "%s method in %s class" % (func.func_name,
                                     self.__class__.__name)
    return func(self, *args, **kwargs)

  return _wrapper(func)

class TestCase:
        @traced
	def run(self):
		self.setUp()
		method = getattr(self, self.name)
		method()

(not tested, and I don't have much experience with decorators...)

-- 
bruno desthuilliers
ruby -e "print 'onurb at xiludom.gro'.split('@').collect{|p|
p.split('.').collect{|w| w.reverse}.join('.')}.join('@')"
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list