Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Help with unittest2 (http://www.velocityreviews.com/forums/t955456-help-with-unittest2.html)

Daniel Laird 12-13-2012 03:03 PM

Help with unittest2
 
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

Miki Tebeka 12-13-2012 03:09 PM

Re: Help with unittest2
 
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/uni...peEqualityFunc) 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)).

Daniel Laird 12-13-2012 03:36 PM

Re: Help with unittest2
 
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/uni...peEqualityFunc) 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?

Boris FELD 12-13-2012 03:48 PM

Re: Help with unittest2
 
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 <daniel.j.laird@googlemail.com>:
> 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/uni...peEqualityFunc) 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


Dave Angel 12-13-2012 03:54 PM

Re: Help with unittest2
 
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


Thomas Bach 12-13-2012 04:04 PM

Re: Help with unittest2
 
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.

Paul Rudin 12-13-2012 04:25 PM

Re: Help with unittest2
 
Thomas Bach <thbach@students.uni-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?


Terry Reedy 12-13-2012 08:50 PM

Re: Help with unittest2
 
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



All times are GMT. The time now is 05:36 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.