Help with unittest2

D

Daniel Laird

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
 
B

Boris FELD

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.
 
D

Dave Angel

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.
 
T

Thomas Bach

Hi,

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.
 
P

Paul Rudin

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?
 
T

Terry Reedy

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.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top