2

我显然对如何在 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)

这不会发生(即,示例代码正确地测试了前面的异常。我需要更改什么才能使这种事情发生?

4

1 回答 1

3

只是有一点点变化。使用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)
于 2013-02-05T04:31:51.983 回答