assertraises

Python assertRaises on user-defined exceptions

主宰稳场 提交于 2020-01-15 05:15:30
问题 The following question was triggered by the discussion in this post. Assume two files ( foobar.py and foobar_unittest.py ). File foobar.py contains a class ( FooBar ) with two functions ( foo and bar ). Function bar raises a built-in exception, function foo a user-defined exception. # foobar.py class MyException(Exception): pass class FooBar: def __init__(self): pass def bar(self): raise ValueError('Hello World.') def foo(self): raise MyException('Hello World.') . # foobar_unittest.py import

assertRaises in python unit-test not catching the exception [duplicate]

一世执手 提交于 2019-11-28 10:47:10
This question already has an answer here: How do you test that a Python function throws an exception? 12 answers Can somebody tell me why the following unit-test is failing on the ValueError in test_bad, rather than catching it with assertRaises and succeeding? I think I'm using the correct procedure and syntax, but the ValueError is not getting caught. I'm using Python 2.7.5 on a linux box. Here is the code … import unittest class IsOne(object): def __init__(self): pass def is_one(self, i): if (i != 1): raise ValueError class IsOne_test(unittest.TestCase): def setUp(self): self.isone = IsOne(

assertRaises in python unit-test not catching the exception [duplicate]

妖精的绣舞 提交于 2019-11-27 03:48:04
问题 This question already has answers here : How do you test that a Python function throws an exception? (13 answers) Closed last year . Can somebody tell me why the following unit-test is failing on the ValueError in test_bad, rather than catching it with assertRaises and succeeding? I think I'm using the correct procedure and syntax, but the ValueError is not getting caught. I'm using Python 2.7.5 on a linux box. Here is the code … import unittest class IsOne(object): def __init__(self): pass