473,396 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

What's the matter with this code section?

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'

Aug 24 '05 #1
2 1526
On 24 Aug 2005 00:18:00 -0700, Johnny Lee <jo************@hotmail.com> wrote:
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.

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Aug 24 '05 #2
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 'o****@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 'o****@xiludom.gro'.split('@')])"
Aug 24 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
8
by: Chris Sharman | last post by:
See the bottom of http://www.prestonbridge.pwp.blueyonder.co.uk/index2.htm (4th line of the club officers section). The mark-up's validated html 4 & css, but in ie5.0/Win2000 my surname...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
53
by: Deniz Bahar | last post by:
I know the basic definition of a sequence point (point where all side effects guaranteed to be finished), but I am confused about this statement: "Between the previous and next sequence point an...
126
by: ramyach | last post by:
Hi friends, I need to write a parallel code in 'C' on the server that is running SGI Irix 6.5. This server supports MIPS Pro C compiler. I don't have any idea of parallel C languages. I looked...
0
by: Thirsty Traveler | last post by:
For some reason, a config file similar to the following config file was throwing an error: <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="RpcQmsTrace"...
0
by: Thirsty Traveler | last post by:
For some reason, a config file similar to the following config file was throwing an error: <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="RpcQmsTrace"...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
13
by: nicolegw | last post by:
I've tried googling for the answer to my question, so my apologies if it's been asked before or is terribly obvious. I'm a volunteer doing data entry for research. My office has an Access...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.