gpt4 book ai didi

Python单元测试断言引发错误

转载 作者:行者123 更新时间:2023-12-01 04:56:17 26 4
gpt4 key购买 nike

我在 Python 中运行单元测试时遇到了这个奇怪的问题: 我使用了assertRaises,并且运行单元测试引发了正确的异常,但测试仍然失败。好吧,我无法真正解释它,请自行查看回溯:

Error
Traceback (most recent call last):
File "/Users/chianti/PycharmProjects/Programming_Project/Part1and4/Part1and4Test.py", line 32, in test_non_alpha_name
self.assertRaises(RestNameContainNonAlphaError, RestaurantName(self.non_alpha_name))
File "/Users/chianti/PycharmProjects/Programming_Project/Part1and4/InputCheck.py", line 29, in __init__
raise RestNameContainNonAlphaError('There are non alphabetic characters that I can not recognize!')
RestNameContainNonAlphaError: There are non alphabetic characters that I can not recognize!


Error
Traceback (most recent call last):
File "/Users/chianti/PycharmProjects/Programming_Project/Part1and4/Part1and4Test.py", line 24, in test_non_string_name
self.assertRaises(InputNotStringError, RestaurantName, self.non_string_name)
File "/Users/chianti/anaconda/lib/python2.7/unittest/case.py", line 473, in assertRaises
callableObj(*args, **kwargs)
File "/Users/chianti/PycharmProjects/Programming_Project/Part1and4/InputCheck.py", line 33, in __init__
raise InputNotStringError('Not String! The input is supposed to be a string type!')
InputNotStringError: Not String! The input is supposed to be a string type!

为什么??????????任何想法表示赞赏!谢谢您

<小时/>

这是我的单元测试:

class RestaurantNameTests(unittest.TestCase):

def setUp(self):

self.non_string_name = 123
self.valid_name = 'Italian rest '
self.non_alpha_name = 'valid ** n'

def tearDown(self):

self.non_string_name = None
self.valid_name = None
self.non_alpha_name = None

def test_non_string_name(self):

with self.assertRaises(InputNotStringError):
RestaurantName(self.non_string_name)

def test_valid_name(self):

self.assertEqual(RestaurantName(self.valid_name).__str__(), 'Italian rest')

def test_non_alpha_name(self):

self.assertRaises(RestNameContainNonAlphaError, RestaurantName(self.non_alpha_name))

如果您需要查看 RestaurantName 的定义,这里是:

class RestaurantName():

def __init__(self, input_contents):

self.name = input_contents

if IsValidString(self.name):

self.no_space_name = self.name.replace(' ', '')

if str.isalpha(self.no_space_name):
pass
else:
raise RestNameContainNonAlphaError('There are non alphabetic characters that I can not recognize!')

else:

raise InputNotStringError('Not String! The input is supposed to be a string type!')

def __repr__(self):

return 'RestaurantName(%s)' % self.name.strip()

def __str__(self):

return self.name.strip()

再次感谢

最佳答案

回溯与您对问题的描述(也不符合您的代码 FWIW)不匹配。您收到的错误位于 test_non_alpha_name() 中,您没有发布该错误,但从错误消息来看,它看起来像:

self.assertRaises(
RestNameContainNonAlphaError,
RestaurantName(self.non_alpha_name)
)

这不是使用 assertRaises() 的正确方法。您必须将 ExpectedExceptionClass, callable, *args, **kw 传递给 assertRaises,并且 argskw 将是传递给你的可调用对象。 IOW你想要:

self.assertRaises(
RestNameContainNonAlphaError,
RestaurantName,
self.non_alpha_name
)

原因很简单:按照您当前的调用方式,异常是在调用 assertRaises 之前触发的。

附注:

  • 你的tearDown方法没用
  • 已经有一个针对错误类型的内置异常,名为 TypeError
  • 还有一个针对错误值的内置异常,名为 ValueError

关于Python单元测试断言引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27299542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com