Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; 'things,': 0.09; '~ethan~': 0.09; 'distinct': 0.16; 'eckhardt': 0.16; 'exception:': 0.16; 'failure.': 0.16; 'raised': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'subject:instance': 0.16; 'subject:type': 0.16; 'subject:unittest': 0.16; 'wrote:': 0.21; 'exception': 0.22; 'header:In-Reply-To:1': 0.22; 'header:User-Agent:1': 0.23; 'code.': 0.24; 'wed,': 0.24; 'test': 0.24; 'error': 0.25; 'opposed': 0.27; 'separate': 0.27; 'second': 0.28; 'testing': 0.28; 'subject:with': 0.28; 'test.': 0.29; 'code': 0.29; 'tests': 0.30; 'skip:s 20': 0.34; 'two': 0.35; 'should': 0.35; "i'm": 0.36; 'sure': 0.36; 'hi!': 0.36; 'some': 0.37; 'unit': 0.38; 'correct': 0.38; 'to:addr:python-list': 0.39; 'to:addr:python.org': 0.40; 'mar': 0.61; 'received:userid': 0.61; '2012': 0.69; '*will*': 0.84; 'bundling': 0.84; 'received:69.41.242': 0.84 Date: Thu, 29 Mar 2012 08:35:16 -0700 From: Ethan Furman User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: python-list@python.org Subject: Re: unittest: assertRaises() with an instance instead of a type References: <4f735345$0$29981$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <4f735345$0$29981$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:3907 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1333036287 news.xs4all.nl 6978 [2001:888:2000:d::a6]:33492 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:22346 Steven D'Aprano wrote: > On Wed, 28 Mar 2012 14:28:08 +0200, 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') > > Secondly, that is not the right way to do this unit test. You are testing > two distinct things, so you should write it as two separate tests: I have to disagree -- I do not see the advantage of writing a second test that *will* fail if the first test fails as opposed to bundling both tests together, and having one failure. ~Ethan~