Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #22297
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.04; 'read.': 0.04; 'none:': 0.07; 'attributes': 0.09; 'parameter': 0.09; 'posting.': 0.09; 'terry': 0.09; 'jan': 0.10; 'better:': 0.16; 'builtin': 0.16; 'eckhardt': 0.16; 'exception,': 0.16; 'exception:': 0.16; 'exceptions': 0.16; 'instance.': 0.16; 'match.': 0.16; 'preserved': 0.16; 'presume': 0.16; 'raised': 0.16; 'received:80.91': 0.16; 'received:80.91.229': 0.16; 'received:gmane.org': 0.16; 'received:list': 0.16; 'received:verizon.net': 0.16; 'reedy': 0.16; 'subclass': 0.16; 'subject:instance': 0.16; 'subject:type': 0.16; 'subject:unittest': 0.16; 'def': 0.20; 'wrote:': 0.21; 'function': 0.22; 'exception': 0.22; 'header:In-Reply-To:1': 0.22; 'header :User-Agent:1': 0.23; 'syntax': 0.23; 'code.': 0.24; 'input': 0.24; 'error': 0.25; 'least': 0.27; 'worked': 0.28; 'subject:with': 0.28; 'allows': 0.29; 'change,': 0.29; 'test.': 0.29; 'code': 0.29; "i'd": 0.29; 'tests': 0.30; 'class.': 0.33; 'mention': 0.33; 'this:': 0.33; 'problem': 0.34; 'none': 0.34; 'skip:s 20': 0.34; 'skip:s 30': 0.34; 'skip:s 40': 0.35; "i'm": 0.36; 'sure': 0.36; 'except': 0.36; 'header:X-Complaints-To:1': 0.36; 'hi!': 0.36; 'but': 0.36; 'some': 0.37; 'something': 0.38; 'called': 0.38; 'received:org': 0.38; 'correct': 0.38; 'to:addr :python-list': 0.39; 'think': 0.40; 'to:addr:python.org': 0.40; 'your': 0.60; 'allowed': 0.61; 'course': 0.61; 'course,': 0.61; 'call': 0.62; 'type': 0.62; 'results': 0.65; 'alternative': 0.65; 'exact': 0.70; 'divide': 0.84; 'here...': 0.84; 'want,': 0.95 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: unittest: assertRaises() with an instance instead of a type |
| Date | Wed, 28 Mar 2012 14:26:48 -0400 |
| References | <pncb49-ur7.ln1@satorlaser.homedns.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | pool-74-109-121-73.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 |
| In-Reply-To | <pncb49-ur7.ln1@satorlaser.homedns.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1088.1332959227.3037.python-list@python.org> (permalink) |
| Lines | 68 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1332959227 news.xs4all.nl 6968 [2001:888:2000:d::a6]:44502 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:22297 |
Show key headers only | View raw
On 3/28/2012 8:28 AM, Ulrich Eckhardt wrote:
> Hi!
>
> I'm currently writing some tests for the error handling of some code. In
> this scenario, I must make sure that both the correct exception is
> raised and that the contained error code is correct:
>
>
> try:
> foo()
> self.fail('exception not raised')
> catch MyException as e:
> self.assertEqual(e.errorcode, SOME_FOO_ERROR)
> catch Exception:
> self.fail('unexpected exception raised')
>
>
> This is tedious to write and read. The docs mention this alternative:
>
>
> with self.assertRaises(MyException) as cm:
> foo()
> self.assertEqual(cm.the_exception.errorcode, SOME_FOO_ERROR)
Exceptions can have multiple attributes. This allows the tester to
exactly specify what attributes to test.
> This is shorter, but I think there's an alternative syntax possible that
> would be even better:
>
> with self.assertRaises(MyException(SOME_FOO_ERROR)):
> foo()
I presume that if this worked the way you want, all attributes would
have to match. The message part of builtin exceptions is allowed to
change, so hard-coding an exact expected message makes tests fragile.
This is a problem with doctest.
> Here, assertRaises() is not called with an exception type but with an
> exception instance. I'd implement it something like this:
>
> def assertRaises(self, exception, ...):
> # divide input parameter into type and instance
> if isinstance(exception, Exception):
> exception_type = type(exception)
> else:
> exception_type = exception
> exception = None
> # call testee and verify results
> try:
> ...call function here...
> except exception_type as e:
> if not exception is None:
> self.assertEqual(e, exception)
Did you use tabs? They do not get preserved indefinitely, so they are
bad for posting.
> This of course requires the exception to be equality-comparable.
Equality comparison is by id. So this code will not do what you want.
You can, of course, write a custom AssertX subclass that at least works
for your custom exception class.
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
unittest: assertRaises() with an instance instead of a type Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-03-28 14:28 +0200
Re: unittest: assertRaises() with an instance instead of a type Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-28 18:07 +0000
Re: unittest: assertRaises() with an instance instead of a type Ben Finney <ben+python@benfinney.id.au> - 2012-03-29 12:55 +1100
Re: unittest: assertRaises() with an instance instead of a type Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-29 06:35 +0000
Re: unittest: assertRaises() with an instance instead of a type Peter Otten <__peter__@web.de> - 2012-03-29 08:55 +0200
Re: unittest: assertRaises() with an instance instead of a type Steve Howell <showell30@yahoo.com> - 2012-03-28 22:50 -0700
Re: unittest: assertRaises() with an instance instead of a type Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-03-29 09:08 +0200
Re: unittest: assertRaises() with an instance instead of a type Peter Otten <__peter__@web.de> - 2012-03-29 09:48 +0200
Re: unittest: assertRaises() with an instance instead of a type Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-30 02:45 +0000
Re: unittest: assertRaises() with an instance instead of a type Ethan Furman <ethan@stoneleaf.us> - 2012-03-30 10:45 -0700
Re: unittest: assertRaises() with an instance instead of a type Ethan Furman <ethan@stoneleaf.us> - 2012-03-29 08:35 -0700
Re: unittest: assertRaises() with an instance instead of a type Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-30 02:53 +0000
Re: unittest: assertRaises() with an instance instead of a type Terry Reedy <tjreedy@udel.edu> - 2012-03-28 14:26 -0400
Re: unittest: assertRaises() with an instance instead of a type Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-03-29 09:28 +0200
Re: unittest: assertRaises() with an instance instead of a type Terry Reedy <tjreedy@udel.edu> - 2012-03-29 11:04 -0400
tabs/spaces (was: Re: unittest: assertRaises() with an instance instead of a type) Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-03-29 09:18 +0200
Re: tabs/spaces (was: Re: unittest: assertRaises() with an instance instead of a type) Roy Smith <roy@panix.com> - 2012-03-29 08:49 -0400
Re: tabs/spaces Dave Angel <d@davea.name> - 2012-03-29 11:16 -0400
Re: tabs/spaces Terry Reedy <tjreedy@udel.edu> - 2012-03-29 11:25 -0400
Re: tabs/spaces Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-03-30 09:05 +0200
Re: tabs/spaces Dave Angel <d@davea.name> - 2012-03-30 08:47 -0400
Re: tabs/spaces Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-04-02 09:12 +0200
Re: tabs/spaces Terry Reedy <tjreedy@udel.edu> - 2012-04-02 03:42 -0400
csiph-web