当测试没有引发预期的异常时,如何显示错误消息? [英] How do you show an error message when a test does not throw an expected exception?
问题描述
import mymod
pre>
import unittest
class MyTestCase(unittest.TestCase):
def test1(self):
self.assertRaises(SomeCoolException,mymod.myfunc,compulsory_argument)
现在,如果未抛出异常,我还想显示一条消息。我怎么做 ?蟒蛇文件不清楚。我在compulsory_argument之后添加了消息,失败了。
编辑:我修改了第一个答案并得到例外。这是我的错误?
import unittest
def sayHelloTo(name):
print(Hello+ name)
class MyTestCase(unittest.TestCase):
def test1(self):
person =John
with self。 assertRaises(异常,我有见地的消息):
sayHelloTo(person)
错误:
错误
追溯(最近的最后一次呼叫):
文件 C:\tests\tester.py,第9行,test1
with self.assertRaises(Exception,My insightful message):
AttributeError:__exit__
解决方案从python 3.3开始, assertRaises 可以用作具有消息的上下文管理器:
import unittest
/ pre>
def sayHelloTo(name):
print(Hello+ name)
class MyTestCase(unittest.TestCase):
def test1(self):
person =John
with self.assertRaises(Exception,msg =My有价值的消息):
sayHelloTo(person)
如果__name__ ==__main__:
unittest.main()
结果
Hello John
F
========================================== ======================
失败:test1(__main __。MyTestCase)
---------- -------------------------------------------------- ----------
追溯(最近的最后一次调用):
文件r.py,第10行,test1
sayHelloTo(person)
AssertionError:异常未提出:我的见解消息
--------------------------------- -------------------------------------
在0.001s $ b $中进行1次测试b
失败(失败= 1)
I am new to python. I wanted to test if my code throws an exception. I got the code from here: How do you test that a Python function throws an exception?
import mymod import unittest class MyTestCase(unittest.TestCase): def test1(self): self.assertRaises(SomeCoolException, mymod.myfunc, compulsory_argument)
Now, I also want to display a message if the exception is not thrown. How do I do that ? The python docs do not mention it clearly. I added the message after "compulsory_argument" and it failed.
EDIT: I tried the first answer with modifications and got an exception. What is my mistake here ?
import unittest def sayHelloTo(name): print("Hello " + name) class MyTestCase(unittest.TestCase): def test1(self): person = "John" with self.assertRaises(Exception, "My insightful message"): sayHelloTo(person)
Error:
Error Traceback (most recent call last): File "C:\tests\tester.py", line 9, in test1 with self.assertRaises(Exception, "My insightful message"): AttributeError: __exit__
解决方案As of python 3.3, assertRaises can be used as a context manager with a message:
import unittest def sayHelloTo(name): print("Hello " + name) class MyTestCase(unittest.TestCase): def test1(self): person = "John" with self.assertRaises(Exception, msg="My insightful message"): sayHelloTo(person) if __name__ == "__main__": unittest.main()
Results in
Hello John F ====================================================================== FAIL: test1 (__main__.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "r.py", line 10, in test1 sayHelloTo(person) AssertionError: Exception not raised : My insightful message ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1)
这篇关于当测试没有引发预期的异常时,如何显示错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!