Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6099 > unrolled thread

Unit testing beginner question

Started byAndrius <andrius.a@gmail.com>
First post2011-05-23 15:30 -0700
Last post2011-05-23 20:19 -0400
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Unit testing beginner question Andrius <andrius.a@gmail.com> - 2011-05-23 15:30 -0700
    Re: Unit testing beginner question Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-23 16:46 -0600
      Re: Unit testing beginner question Roy Smith <roy@panix.com> - 2011-05-23 20:19 -0400

#6099 — Unit testing beginner question

FromAndrius <andrius.a@gmail.com>
Date2011-05-23 15:30 -0700
SubjectUnit testing beginner question
Message-ID<b34bbd5b-bd87-43e0-85a9-1cfbdcc4ca8c@y12g2000yqh.googlegroups.com>
Hello,

would be gratefull for the explonation.

I did a simple test case:

def setUp(self):
  self.testListNone = None

def testListSlicing(self):
  self.assertRaises(TypeError, self.testListNone[:1])

and I am expecting test to pass, but I am getting exception:
Traceback (most recent call last):
    self.assertRaises(TypeError, self.testListNone[:1])
TypeError: 'NoneType' object is unsubscriptable

I thought that assertRaises will pass since TypeError exception will
be raised?

Ta,
Andrius


[toc] | [next] | [standalone]


#6101

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-05-23 16:46 -0600
Message-ID<mailman.1991.1306191316.9059.python-list@python.org>
In reply to#6099
On Mon, May 23, 2011 at 4:30 PM, Andrius <andrius.a@gmail.com> wrote:
> and I am expecting test to pass, but I am getting exception:
> Traceback (most recent call last):
>    self.assertRaises(TypeError, self.testListNone[:1])
> TypeError: 'NoneType' object is unsubscriptable
>
> I thought that assertRaises will pass since TypeError exception will
> be raised?

The second argument to assertRaises must be a function that
assertRaises will call.  assertRaises can't catch the error above
because it is raised when the argument is evaluated, before
assertRaises has even been called.

This would work:

self.assertRaises(TypeError, lambda: self.testListNone[:1])

Cheers,
Ian

[toc] | [prev] | [next] | [standalone]


#6105

FromRoy Smith <roy@panix.com>
Date2011-05-23 20:19 -0400
Message-ID<roy-171A63.20190123052011@news.panix.com>
In reply to#6101
In article <mailman.1991.1306191316.9059.python-list@python.org>,
 Ian Kelly <ian.g.kelly@gmail.com> wrote:

> This would work:
> 
> self.assertRaises(TypeError, lambda: self.testListNone[:1])

If you're using the version of unittest from python 2.7, there's an even 
nicer way to write this:

with self.assertRaises(TypeError):
    self.testListNone[:1]

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web