【问题标题】:I am trying to run unittest using python. But, when I try to run the below code, I get error even though I have handled the exception我正在尝试使用 python 运行单元测试。但是,当我尝试运行下面的代码时,即使我已经处理了异常,我也会收到错误
【发布时间】:2019-07-28 18:48:10
【问题描述】:
import unittest
import math

class Circle:
    def __init__(self, radius):
        self.radius = radius
        # Define the initialization method below
        try:
            if not isinstance(radius, (int, float)):
                raise TypeError
        except TypeError:
            print("radius must be a number")
        try:
            if radius in range(0, 1001):
                raise ValueError
        except ValueError:
            print("radius must be between 0 and 1000 inclusive")

    def area(self):
        return round(math.pi * self.radius ** 2, 2)

    def circumference(self):
        return round(2 * math.pi * self.radius)


class TestCircleCreation(unittest.TestCase):
    def test_creating_circle_with_numeric_radius(self):
        # Define a circle 'c1' with radius 2.5 and check if
        # the value of c1.radius equal to 2.5 or not
        c1 = Circle(2.5)
        self.assertEqual(2.5, c1.radius)

    def test_creating_circle_with_negative_radius(self):
        # Try Defining a circle 'c' with radius -2.5 and see
        # if it raises a ValueError with the message
        # "radius must be between 0 and 1000 inclusive"
        c2 = Circle(-2.5)
        self.assertRaises(ValueError, c2)

    def test_creating_circle_with_greaterthan_radius(self):
        # Try Defining a circle 'c' with radius 1000.1 and see
        # if it raises a ValueError with the message
        # "radius must be between 0 and 1000 inclusive"
        c3 = Circle(1000.1)
        self.assertRaises(ValueError, c3)

    def test_creating_circle_with_nonnumeric_radius(self):
        # Try Defining a circle 'c' with radius 'hello' and see
        # if it raises a TypeError with the message
        # "radius must be a number"
        c4 = Circle("hello")
        self.assertRaises(TypeError, c4)

【问题讨论】:

  • 什么错误?你正在捕捉ValueErrors,但你也断言它们会被抛出。我猜你会收到AssertionErrors?你为什么要抛出 try 阻止 try 捕获的相同异常?
  • 我得到了 'TypeError: 'Circle' object is not callable' 对于上述两个测试:'test_creating_circle_with_negative_radius' 和 'test_creating_circle_with_greaterthan_radius'。
  • 以后,请格式化您的代码并添加足够的错误描述。让别人难以帮助你并不是获得帮助的好方法。
  • 这是我在 Stack 上提出的第一个问题,因为我是编程新手。以后我会尝试更清楚地表达我的问题。

标签: python-3.x exception python-unittest


【解决方案1】:

assertRaises expects a function for it's second parameter. 你传递了一个Circle 对象,它试图将其作为函数调用;这是导致该错误的原因。

我想你的意思是这样的:

self.assertRaises(ValueError, lambda: Circle(-2.5))

然后对其他情况进行类似的更改。

这将对Circle 构造函数的调用包装在lambda 函数中,因此assertRaises 可以根据需要调用构造函数。


这仍然会失败,因为您使用 try 来捕获 assertRaises 正在寻找的 ValueErrorTypeErrors。如果您希望抛出异常,请不要在内部捕获它们。

【讨论】:

  • 那么为什么测试“test_creating_circle_with_nonnumeric_radius”有效?因为我在那里也使用了相同的逻辑。
  • @TejaswaYadav 我认为它不起作用。我认为脚本在该代码执行之前失败,因此该函数永远不会运行。
  • 它正在工作。上述两个测试正在运行并通过。 'test_creating_circle_with_numeric_radius' 和 'test_creating_circle_with_nonnumeric_radius' 工作正常。
  • @TejaswaYadav 使用assertEquals 的测试用例会起作用,因为它不需要函数。 TypeError 测试实际上似乎有效,因为正如您在评论中指出的那样,尝试将非函数作为函数调用会引发TypeError,您正在使用assertRaises 检查。它提高了TypeError,因为assertRaises 的参数不好,而不是因为您的代码正确地引发了错误。测试未正确通过。
  • 好的。那么我该如何纠正这个错误呢?
猜你喜欢
  • 2021-08-07
  • 2020-10-27
  • 2021-12-07
  • 2019-10-24
  • 2020-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-18
相关资源
最近更新 更多