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


Groups > comp.lang.python > #25416

Re: assertraises behaviour

From Christian Heimes <lists@cheimes.de>
Subject Re: assertraises behaviour
Date 2012-07-16 15:55 +0200
References <CAF_E5JYANst57yOud+Ot0AbKkePruoCGPUNC5BbyBoMbgaw2_Q@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2173.1342446935.4697.python-list@python.org> (permalink)

Show all headers | View raw


Am 16.07.2012 15:38, schrieb andrea crotti:
> This small example doesn't fail, but the OSError exception is cathed
> even if not declared..
> Is this the expected behaviour (from the doc I would say it's not).
> (Running on arch-linux 64 bits and Python 2.7.3, but it doesn the same
> with Python 3.2.3)
> 
> import unittest
> 
> class TestWithRaises(unittest.TestCase):
>     def test_ass(self):
>         with self.assertRaises(AssertionError):
>             assert False, "should happen"
>             raise OSError("should give error")

The OSError isn't catched as the code never reaches the line with "raise
OSError". In other words "raise OSError" is never executed as the
exception raised by "assert False" stops the context manager.

You should avoid testing more than one line of code in a with
self.assertRaises() block.

Christian

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: assertraises behaviour Christian Heimes <lists@cheimes.de> - 2012-07-16 15:55 +0200

csiph-web