unittest behaving oddly

Discussion in 'Python' started by David Vincent, Jun 20, 2006.

  1. -----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
    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-----
    David Vincent, Jun 20, 2006
    #1
    1. Advertising

  2. David Vincent

    Mike Kent Guest

    David Vincent wrote:

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


    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)
    Mike Kent, Jun 20, 2006
    #2
    1. Advertising

  3. -----BEGIN PGP SIGNED MESSAGE-----

    On 20/06/2006, at 23:15, Mike Kent wrote:

    > David Vincent wrote:
    >
    >>> import unittest
    >>>
    >>> class IntegerArithmenticTestCase(unittest.TestCase):
    >>> def testAdd(self): ## test method names begin 'test*'
    >>> assertEquals((1 + 2), 3)
    >>> assertEquals(0 + 1, 1)

    >
    > 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
    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-----
    David Vincent, Jun 20, 2006
    #3
  4. David Vincent wrote:

    > 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>
    Fredrik Lundh, Jun 20, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben
    Replies:
    7
    Views:
    298
    Steven D'Aprano
    Dec 28, 2006
  2. Replies:
    8
    Views:
    367
    mlimber
    Dec 8, 2005
  3. Siam
    Replies:
    4
    Views:
    402
    Pete Becker
    Jul 27, 2006
  4. Replies:
    13
    Views:
    179
    Ilya Zakharevich
    Feb 22, 2011
  5. babygodzilla
    Replies:
    4
    Views:
    107
    babygodzilla
    Sep 18, 2007
Loading...

Share This Page