I have found many examples of how to assert something was logged, for example http://www.michaelpollmeier.com/python-mock-how-to-assert-a-substring-of-logger-output
However I don't know how to decouple the assertion from the specific way the message was constructed. The test only cares about specific ids being logged.
Test Code
mock_logger.warn.assert_called_with(
all_match(
contains_string('user-id'),
contains_string('team-id')
)
)
Should work for both
Production Code 1 (logger assembles the message):
logger.warn(
"Order for team %s and user %s could not be processed",
'team-id',
'user-id'
)
and
Production Code 2 (we assemble the message and include exception):
logger.warn(
"Order for team {} and user {} could not be processed"
.format('team-id', 'user-id'),
ex
)
This won't work as is but I'm thinking of either capturing the arguments or setting a custom log appender and making the assertions on the final messages.
Please ignore any typos / potential syntax errors as I haven't written the code in an IDE