Help with unittest2

Discussion in 'Python' started by Daniel Laird, Dec 13, 2012.

  1. Daniel Laird

    Daniel Laird Guest

    All,

    I am new to python and am stuck with python 2.6 (Ubuntu 10.04 and dont want to force switch to 2.7)
    I want to use assertListEqual and other new test functions.
    However
    I do am import unittest2 as unittest
    The code does not fail but any use of the new functions results in:
    NameError: global name 'assertListEqual' is not defined

    What am I doing wrong?
    Do I need to change something else? The docs seem to imply what I have done is enough.

    Hope you can help
    Cheers
    Dan L
    Daniel Laird, Dec 13, 2012
    #1
    1. Advertising

  2. Daniel Laird

    Miki Tebeka Guest

    Miki Tebeka, Dec 13, 2012
    #2
    1. Advertising

  3. Daniel Laird

    Daniel Laird Guest

    On Thursday, December 13, 2012 3:09:58 PM UTC, Miki Tebeka wrote:
    > On Thursday, December 13, 2012 7:03:27 AM UTC-8, Daniel Laird wrote:
    >
    > > I do am import unittest2 as unittest

    >
    > > NameError: global name 'assertListEqual' is not defined

    >
    > According to the docs (http://docs.python.org/2/library/unittest.html#unittest.TestCase.addTypeEqualityFunc) assertListEqual and friends was added in 2.7.
    >
    >
    >
    > You can use assertEuqal, or if you don't care about order assertEqual(sorted(a), sorted(b)).


    Thanks, however I thought by using unittest2 it added the new 2.7 features to 2.6?
    Daniel Laird, Dec 13, 2012
    #3
  4. Daniel Laird

    Boris FELD Guest

    How are you importing unittest2, do you have something like this ?

    try:
    import unittest2 as unittest
    except ImportError:
    import unittest

    If it's the case, you are maybe using default unittest while you think
    you are using unittest2.

    2012/12/13 Daniel Laird <>:
    > On Thursday, December 13, 2012 3:09:58 PM UTC, Miki Tebeka wrote:
    >> On Thursday, December 13, 2012 7:03:27 AM UTC-8, Daniel Laird wrote:
    >>
    >> > I do am import unittest2 as unittest

    >>
    >> > NameError: global name 'assertListEqual' is not defined

    >>
    >> According to the docs (http://docs.python.org/2/library/unittest.html#unittest.TestCase.addTypeEqualityFunc) assertListEqual and friends was added in 2.7.
    >>
    >>
    >>
    >> You can use assertEuqal, or if you don't care about order assertEqual(sorted(a), sorted(b)).

    >
    > Thanks, however I thought by using unittest2 it added the new 2.7 features to 2.6?
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    Boris FELD, Dec 13, 2012
    #4
  5. Daniel Laird

    Dave Angel Guest

    On 12/13/2012 10:03 AM, Daniel Laird wrote:
    > All,
    >
    > I am new to python and am stuck with python 2.6 (Ubuntu 10.04 and dont want to force switch to 2.7)
    > I want to use assertListEqual and other new test functions.
    > However
    > I do am import unittest2 as unittest
    > The code does not fail but any use of the new functions results in:
    > NameError: global name 'assertListEqual' is not defined
    >
    > What am I doing wrong?
    > Do I need to change something else? The docs seem to imply what I have done is enough.


    Where's your code? And the full traceback from the error? From what
    you say, I can't imagine how it would work, so my mind assumes something
    different. You should be able to demonstrate the problem with a two
    line sample program. Pasted from a real test, not paraphrased.

    You do know that you'll need
    unittest.assertListEqual()

    right? That's because you used
    import unittest2 as unittest

    rather than
    from unittest2 import assertListEqual


    You can tell what names are in the unittest namespace by doing a
    dir(unittest). Although that's normally done interactively, it'll work
    fine from your code, as long as you put it before the spot where the
    exception happens.



    --

    DaveA
    Dave Angel, Dec 13, 2012
    #5
  6. Daniel Laird

    Thomas Bach Guest

    Hi,

    On Thu, Dec 13, 2012 at 07:03:27AM -0800, Daniel Laird wrote:
    > I do am import unittest2 as unittest
    > The code does not fail but any use of the new functions results in:
    > NameError: global name 'assertListEqual' is not defined
    >
    > What am I doing wrong?


    Read the error message again: it says that it cannot find the _global_
    name 'assertListEqual'!

    assertListEqual is a method of unittest.TestCase. Hence, it has to be
    called on a unittest.TestCase instance, such as

    import unittest

    class FooTests(unittest.TestCase):

    def test_a_list(self):
    a = ['foo', 'bar']
    b = ['foo', 'bar']
    self.assertListEqual(a, b)

    BTW, I actually never used 'assertTypeEqual'. I rather call
    assertEqual and let unittest do the internals. I think assertEqual
    calls the right method for you depending on the arguments type. If you
    want to make sure that something is of a certain type use
    assertIsInstance!

    Hope this helps,

    Thomas Bach.
    Thomas Bach, Dec 13, 2012
    #6
  7. Daniel Laird

    Paul Rudin Guest

    Thomas Bach <-mainz.de> writes:


    > BTW, I actually never used 'assertTypeEqual' I rather call assertEqual
    > and let unittest do the internals. I think assertEqual calls the right
    > method for you depending on the arguments type.



    The assert<Type>Equal methods have the advantage of checking the type of
    the arguments. assertEqual would be OK with equal numerical arguments,
    but that would be an inferior test if you were really expecting two
    lists.

    > If you want to make sure that something is of a certain type use
    > assertIsInstance!


    Yes, but why do something in 3 lines when there's a perfectly good
    method provided that allows you do to it directly in one?
    Paul Rudin, Dec 13, 2012
    #7
  8. Daniel Laird

    Terry Reedy Guest

    On 12/13/2012 10:03 AM, Daniel Laird wrote:
    > All,
    >
    > I am new to python and am stuck with python 2.6 (Ubuntu 10.04 and
    > dont want to force switch to 2.7)


    You can altinstall 2.7 and leave the system 2.6 alone.


    --
    Terry Jan Reedy
    Terry Reedy, Dec 13, 2012
    #8
    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. =?Utf-8?B?dHJlbGxvdzQyMg==?=

    HELP! HELP! HELP! Opening Web Application Project Error

    =?Utf-8?B?dHJlbGxvdzQyMg==?=, Feb 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    765
    =?Utf-8?B?dHJlbGxvdzQyMg==?=
    Feb 20, 2004
  2. Harvey
    Replies:
    0
    Views:
    696
    Harvey
    Jul 16, 2004
  3. Harvey
    Replies:
    1
    Views:
    840
    Daniel
    Jul 16, 2004
  4. =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?=

    HELP - HELP - HELP

    =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?=, Mar 9, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    681
    Eliyahu Goldin
    Mar 9, 2006
  5. Andrea Crotti

    Unittest2 on python 2.6

    Andrea Crotti, Mar 18, 2012, in forum: Python
    Replies:
    0
    Views:
    166
    Andrea Crotti
    Mar 18, 2012
Loading...

Share This Page