unittest behaving oddly

D

David Vincent

-----BEGIN PGP SIGNED MESSAGE-----

Hello


I'm hoping to get some insight into a situation that seems odd to me.
My Python experience is limited; I've just started using the unittest
module. I've had some experience with unit test support in other
languages.

Two computers that I use are similarly configured; each has Python 2.3
installed as distributed with the operating system, as far as I can
tell. The operating system is Mac OS X 10.3.9 in each case. The
hardware differs; they are both PowerPC computers. On these machines
unittest is behaving oddly, both in the same way.

If I copy the example from the unittest documentation and feed it to
Python, I get errors about methods in unittest. I think the example is
intended to pass its tests, but on my machines the code in the example
is not locating methods from unittest properly.

I've done some simple things, like inspecting the unittest code in the
library; it seems to make sense. For instance, the 'assertEquals'
method is defined as a member of TestCase.

What could be going wrong? I wonder if I should try to track down
problems in the example, or in the Python library installation on these
machines, or somewhere else.

Below is a transcript illustrating the problem.


Regards

David


PS: transcript illustrating the problem
Mina:~/Documents/source/unittest-debug dvincent$ cat example.py
# unittest example from documentation

import unittest

class IntegerArithmenticTestCase(unittest.TestCase):
def testAdd(self): ## test method names begin 'test*'
assertEquals((1 + 2), 3)
assertEquals(0 + 1, 1)
def testMultiply(self):
assertEquals((0 * 10), 0)
assertEquals((5 * 8), 40)

if __name__ == '__main__':
unittest.main()

Mina:~/Documents/source/unittest-debug dvincent$ python example.py
EE
======================================================================
ERROR: testAdd (__main__.IntegerArithmenticTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "example.py", line 7, in testAdd
assertEquals((1 + 2), 3)
NameError: global name 'assertEquals' is not defined

======================================================================
ERROR: testMultiply (__main__.IntegerArithmenticTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "example.py", line 10, in testMultiply
assertEquals((0 * 10), 0)
NameError: global name 'assertEquals' is not defined

----------------------------------------------------------------------
Ran 2 tests in 0.045s

FAILED (errors=2)
Mina:~/Documents/source/unittest-debug dvincent$ python -V
Python 2.3
Mina:~/Documents/source/unittest-debug dvincent$



- --

David A Vincent (e-mail address removed)
Software Engineer telephone +61 2 9850-2593
RESEARCH AND DEVELOPMENT TOSHIBA (AUSTRALIA) PTY. LIMITED
"The situation is hopeless, we must take the next step." P.Casals
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (Darwin)

iQCVAwUBRJfutpjExRNGDpoZAQFb/QP9H360XupArE3NuLZvI3PPpPIs2Ht9JcLw
P7hY1iz9Bl7A/7RBNVjJeegYByJwHX+8Zlgb/uBXMlTyS8KiCsILOgeT3v+ycaZg
W7VKcY4lCm1FZJRJkGvvwKtySTb54uuLoGU6kPEqrBS0vHxbhNrKacT4vNZN9UM4
NNBttvVDijk=
=1l9n
-----END PGP SIGNATURE-----
 
M

Mike Kent

assertEquals is a member function, inherited from unittest.TestCase, so
you must call it as self.assertEquals. ForEx:

class IntegerArithmenticTestCase(unittest.TestCase):
def testAdd(self): ## test method names begin 'test*'
self.assertEquals((1 + 2), 3)
 
D

David Vincent

-----BEGIN PGP SIGNED MESSAGE-----

assertEquals is a member function, inherited from unittest.TestCase, so
you must call it as self.assertEquals. ForEx:

(Details deleted)


Thank you Mike. That fixes most of my problems.

The code quoted above was from my verbatim copy of the example from the
doc strings of unittest.py, from my computer's Python library. I
wonder how the example code could have been so badly wrong?


regards

David

- --

David A Vincent (e-mail address removed)
Software Engineer telephone +61 2 9850-2593
RESEARCH AND DEVELOPMENT TOSHIBA (AUSTRALIA) PTY. LIMITED
"The situation is hopeless, we must take the next step." P.Casals

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (Darwin)

iQCVAwUBRJf/7pjExRNGDpoZAQEaKgQAgCVSdE/WJCunEdPfEIgCHk+dKMJHc2B6
XWfc3WPvUcH5RL4dQ18bbKNHEoHSZbMrN4CLKD55LueeaPbIgFgOpSkwoRRoakDi
LWNbn7OWz2G1mbNgEVvqkOBhfK5l582kjzndtdsPBPD+Pv/KiEC77/6dN27Fnl6e
d0oKoDD3aac=
=/dqt
-----END PGP SIGNATURE-----
 
F

Fredrik Lundh

David said:
The code quoted above was from my verbatim copy of the example from the
doc strings of unittest.py, from my computer's Python library. I
wonder how the example code could have been so badly wrong?

self-eating viruses on your machine ?

$ more unittest.py

....

Simple usage:

import unittest

class IntegerArithmenticTestCase(unittest.TestCase):
def testAdd(self): ## test method names begin 'test*'
self.assertEquals((1 + 2), 3)
self.assertEquals(0 + 1, 1)
def testMultiply(self):
self.assertEquals((0 * 10), 0)
self.assertEquals((5 * 8), 40)

....

</F>
 

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

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top