What's the matter with this code section?

Johnny Lee johnnyandfiona at hotmail.com
Wed Aug 24 09:18:00 CEST 2005


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'




More information about the Python-list mailing list