Python测试提升值错误

Python测试提升值错误,python,Python,我只是在使用常规库(不是pytest)并尝试确认ValueError 测试: 结果: if zone not in pytz.all_timezones: raise ValueError("Invalid Time Zone Used: " + time_zone) >>> ValueError: Invalid Time Zone Used: US/Eas2tern 我想用类似的结构来测试它 Assert "valueError" etc... 如果您使用u

我只是在使用常规库(不是pytest)并尝试确认ValueError

测试:

结果:

if zone not in pytz.all_timezones:
    raise ValueError("Invalid Time Zone Used: " + time_zone)

>>> ValueError: Invalid Time Zone Used: US/Eas2tern
我想用类似的结构来测试它

Assert "valueError"  etc...

如果您使用unittest之类的测试包,您将有最轻松的时间,请参阅本文了解如何做到这一点:

但是,如果您不想使用任何测试包,您可以执行以下操作(基本上是将ValueError的缺失转换为AssertionError,如果您得到ValueError,则什么也不做):

Assert "valueError"  etc...
def test():
    data = get_time_zone("sun/10-21 tz:US/Eas2tern")

try:
    test()
    raise AssertionError
except ValueError:
    pass