单元测试-如何在python中创建/实现/测试一个新的异常



我显然对如何在python中引发异常有一些根本性的误解。我列举了一个最简单的例子来说明我试图做什么(但失败了)。我试图创建一个新的异常,并正确地测试它是否有效。

import random
import unittest
# Create new class of exception
class LearningError(Exception):
    pass
# Create function
def addition_no_four(first, second):
    """Add two numbers (as long as it isn't 4)."""
    if (first == 4) or (second == 4):
        raise LearningError("We don't take 4s!")
    return first + second
# Properly working example code that tests raising errors
class TestSequenceFunctions(unittest.TestCase):
    def setUp(self):
        self.seq = range(10)
    def test_shuffle(self):
        random.shuffle(self.seq)
        self.seq.sort()
        self.assertEqual(self.seq, range(10))
        self.assertRaises(TypeError, random.shuffle, (1,2,3))
# My code which tests 
class TestAddition(unittest.TestCase):
    def test_addition(self):
        """Test whether it works for 2 numbers (not 4)."""
        first = 2
        second = 5
        self.assertEqual(addition_no_four(first, second), 7)
    def test_raise(self):
        """Learn how to create an exception and test its implementation."""
        self.assertRaises(LearningError, addition_no_four(2, 4))
if __name__ == "__main__":
    unittest.main()

失败,显示以下消息:

Traceback (most recent call last):
  File "test.py", line 34, in test_raise
    self.assertRaises(LearningError, addition_no_four(2, 4))
  File "test.py", line 12, in addition_no_four
    raise LearningError("We don't take 4s!")
LearningError: We don't take 4s!
----------------------------------------------------------------------
Ran 3 tests in 0.000s
FAILED (errors=1)

这并没有发生(例如,示例代码正确地测试了前面的异常。我需要更改什么才能发生这种情况?

只有一个小的变化。使用assertRaises时,请确保直接调用函数。相反,它的参数需要作为参数传递给assertRaises。这允许assertRaises测试方法在调用函数之前设置try/except。

def test_raise(self):
    """Learn how to create an exception and test its implementation."""
    self.assertRaises(LearningError, addition_no_four, 2, 4)

您也可以通过使用assertRaises作为内容管理器来绕过这个问题:

def test_raise(self):
    """Learn how to create an exception and test its implementation."""
    with self.assertRaises(LearningError):
        addition_no_four(2, 4)

相关内容

  • 没有找到相关文章

最新更新