Reputation: 2030
My python unit test calls
self.assertRaisesRegex(Exception, "Missing price data for hids: {123}", self.idc_service.load_prices, date(2021, 2, 22), date(2021, 2, 23), [123])
which results in the error:
Failure
Exception: Missing price data for hids: {123}
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\ProgramData\Miniconda3\lib\unittest\case.py", line 59, in testPartExecutor
yield
File "C:\ProgramData\Miniconda3\lib\unittest\case.py", line 615, in run
testMethod()
File "C:\Users\Alin\Desktop\code\pyApps\pyApps\tests\services\TestIdcDataService.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:\ProgramData\Miniconda3\lib\unittest\case.py", line 1285, in assertRaisesRegex
return context.handle('assertRaisesRegex', args, kwargs)
File "C:\ProgramData\Miniconda3\lib\unittest\case.py", line 178, in handle
callable_obj(*args, **kwargs)
File "C:\ProgramData\Miniconda3\lib\unittest\case.py", line 217, in __exit__
expected_regex.pattern, str(exc_value)))
File "C:\ProgramData\Miniconda3\lib\unittest\case.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}"
The assertion error is confusing because those 2 strings match exactly. Is something going on with the message 'During handling of the above exception, another exception occurred'?
Upvotes: 1
Views: 1033
Reputation: 5387
{} has special meaning for regexes, you need to escape them.
self.assertRaisesRegex(Exception, r"Missing price data for hids: \{123\}", self.idc_service.load_prices, date(2021, 2, 22), date(2021, 2, 23), [123])
Upvotes: 2