我的python单元测试调用
self.assertRaisesRegex(Exception, "Missing price data for hids: {123}", self.idc_service.load_prices, date(2021, 2, 22), date(2021, 2, 23), [123])
导致错误:
Failure
Exception: Missing price data for hids: {123}
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:ProgramDataMiniconda3libunittestcase.py", line 59, in testPartExecutor
yield
File "C:ProgramDataMiniconda3libunittestcase.py", line 615, in run
testMethod()
File "C:UsersAlinDesktopcodepyAppspyAppstestsservicesTestIdcDataService.py", line 32, in test_missing_hid_prices
self.assertRaisesRegex(Exception, "Missing price data for hids: {123}", self.idc_service.load_prices, date(2021, 2, 22), date(2021, 2, 23), hids)
File "C:ProgramDataMiniconda3libunittestcase.py", line 1285, in assertRaisesRegex
return context.handle('assertRaisesRegex', args, kwargs)
File "C:ProgramDataMiniconda3libunittestcase.py", line 178, in handle
callable_obj(*args, **kwargs)
File "C:ProgramDataMiniconda3libunittestcase.py", line 217, in __exit__
expected_regex.pattern, str(exc_value)))
File "C:ProgramDataMiniconda3libunittestcase.py", line 135, in _raiseFailure
raise self.test_case.failureException(msg)
AssertionError: "Missing price data for hids: {123}" does not match "Missing price data for hids: {123}"
断言错误令人困惑,因为这两个字符串完全匹配。"在处理上述异常期间,发生了另一个异常"的消息是否正在发生?
{}对于正则表达式有特殊的含义,您需要转义它们。
self.assertRaisesRegex(Exception, r"Missing price data for hids: {123}", self.idc_service.load_prices, date(2021, 2, 22), date(2021, 2, 23), [123])