What's the matter with this code section?

J

Johnny Lee

Here is the source:

#! /bin/python

#@brief This is a xunit test framework for python, see TDD for more
details

class TestCase:
def setUp(self):
print "setUp in TestCase"
pass
def __init__(self, name):
print "__init__ in TestCase"
self.name = name
def run(self):
print "run in TestCase"
self.setUp()
method = getattr(self, self.name)
method()

class WasRun(TestCase):
def __init__(self, name):
print "__init__ in WasRun"
self.wasRun = None
TestCase.__init__(self, name)
def testMethod(self):
print "testMethod in WasRun"
self.wasRun = 1
def run(self):
print "run in WasRun"
method = getattr(self, self.name)
method()
def setUp(self):
print "in setUp of WasRun"
self.wasSetUp = 1

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()
assert(test.wasSetUp)

# the program starts here
print "starts TestCaseTest(\"testRunning\").run()"
TestCaseTest("testRunning").run()
print "starts TestCaseTest(\"testSetUp\").run()"
TestCaseTest("testSetUp").run()



And here is the result running under cygwin:

$ ./xunit.py
starts TestCaseTest("testRunning").run()
__init__ in TestCase
run in TestCase
setUp in TestCase
testRunning in TestCaseTest
__init__ in WasRun
__init__ in TestCase
run in WasRun
testMethod in WasRun
starts TestCaseTest("testSetUp").run()
__init__ in TestCase
run in TestCase
setUp in TestCase
testSetUp in TestCaseTest
__init__ in WasRun
__init__ in TestCase
run in WasRun
testMethod in WasRun
Traceback (most recent call last):
File "./xunit.py", line 51, in ?
TestCaseTest("testSetUp").run()
File "./xunit.py", line 16, in run
method()
File "./xunit.py", line 45, in testSetUp
assert(test.wasSetUp)
AttributeError: WasRun instance has no attribute 'wasSetUp'
 
S

Simon Brunning

AttributeError: WasRun instance has no attribute 'wasSetUp'

It means exactly what it says. The last line of your TestCaseTest
classes testSetUp method refers to test.wasSetUp. So far as I can see,
test is an instance of your WasRun class, and I don't see a wasSetUp
attribute being set anywhere.
 
B

bruno modulix

Johnny said:
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 '(e-mail address removed)'.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 '(e-mail address removed)'.split('@')])"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,315
Members
47,996
Latest member
LaurenFola

Latest Threads

Top