Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #12222 > unrolled thread
| Started by | lblake <treleven.lloyd@gmail.com> |
|---|---|
| First post | 2011-08-26 08:35 -0700 |
| Last post | 2011-08-26 16:52 +0100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
Unit test failing please help lblake <treleven.lloyd@gmail.com> - 2011-08-26 08:35 -0700
Re: Unit test failing please help John Gordon <gordon@panix.com> - 2011-08-26 15:58 +0000
Re: Unit test failing please help Tim Wintle <tim.wintle@teamrubber.com> - 2011-08-26 16:52 +0100
| From | lblake <treleven.lloyd@gmail.com> |
|---|---|
| Date | 2011-08-26 08:35 -0700 |
| Subject | Unit test failing please help |
| Message-ID | <ccbce61b-77e3-44fc-bbb8-fbd7007326dd@w28g2000yqw.googlegroups.com> |
Hi I am new to python I am at bit lost as to why my unit test is
failing below is the code and the unit test:
class Centipede(object):
legs, stomach
def __init__(self):
def __str__(self):
return ','.join(self.stomach)
def __call__(self,*args):
[self.stomach.append(arg) for arg in args]
#self.stomach.append(args)
def __repr__(self):
return ','.join(self.legs)
def __setattr__(self, key, value):
print("setting %s to %s" % (key, repr(value)))
if key in ([]):
self.legs.append(key)
super(Centipede, self).__setattr__(self, key,value)
unit test code:
import unittest
from centipede import Centipede
class TestBug(unittest.TestCase):
def test_stomach(self):
ralph = Centipede()
ralph('chocolate')
ralph('bbq')
ralph('cookies')
ralph('salad')
self.assertEquals(ralph.__str__(),
'chocolate,bbq,cookies,salad')
def test_legs(self):
ralph = Centipede()
ralph.friends = ['Steve', 'Daniel', 'Guido']
ralph.favorite_show = "Monty Python's Flying Circus"
ralph.age = '31'
self.assertEquals(ralph.__repr__(),'<friends,favorite_show,age>' )
if __name__ == "__main__":
unittest.main()
error message generated when running the test:
AttributeError: 'Centipede' object has no attribute 'legs'
AttributeError: 'Centipede' object has no attribute 'stomach'
Any suggestions as to where I am going wrong?
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2011-08-26 15:58 +0000 |
| Message-ID | <j38fqd$eqn$1@reader1.panix.com> |
| In reply to | #12222 |
In <ccbce61b-77e3-44fc-bbb8-fbd7007326dd@w28g2000yqw.googlegroups.com> lblake <treleven.lloyd@gmail.com> writes:
> Hi I am new to python I am at bit lost as to why my unit test is
> failing below is the code and the unit test:
> class Centipede(object):
> legs, stomach
You aren't assigning any values to "legs" or "stomach" here. From your
later code, it seems like you intend these items to start out as empty
lists. This code might work better:
class Centipede(object):
legs = []
stomach = []
(In fact, since you aren't *assigning* anything to legs or stomach but
you're simply *referencing them*, this code should have been an error
because those names do not yet exist.)
> def __init__(self):
This __init__() method does nothing at all. It doesn't even have a pass
statement, which means it isn't legal python. Your code should have
produced an error here.
> def __setattr__(self, key, value):
> print("setting %s to %s" % (key, repr(value)))
> if key in ([]):
> self.legs.append(key)
> super(Centipede, self).__setattr__(self, key,value)
How will this if statement ever be true? You're checking if 'key' is
a member of an empty list.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Tim Wintle <tim.wintle@teamrubber.com> |
|---|---|
| Date | 2011-08-26 16:52 +0100 |
| Message-ID | <mailman.449.1314376532.27778.python-list@python.org> |
| In reply to | #12222 |
On Fri, 2011-08-26 at 08:35 -0700, lblake wrote:
> Hi I am new to python I am at bit lost as to why my unit test is
> failing below is the code and the unit test:
>
> class Centipede(object):
> legs, stomach
This doesn't do what you think it does.
"legs, stomach" is a statement and is not defining any variables at all.
Presumably you've also got variables named legs and stomach in the
module's scope - as I'd expect to see a NameError : name 'legs' is not
defined.
(I'd also expect a SyntaxError from having an empty __init__ function
body)
You probably want do write something like this:
class Centipede(object):
def __init__(self):
self.legs = []
self.stomach = []
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web