I'd just do
comment = results.to_dict()["comment"]
assert comment.startswith("Retrying in 3600 seconds")
though the effect is slightly different, as you're not checking whether "comment"
is the only key in the dict anymore. If you need that, you could
result = results.to_dict()
comment = result.pop("comment")
# (Pop out more keys)
assert not result # nothing left unread
assert comment.startswith("Retrying in 3600 seconds")
And, then, of course, if you really do want to test whether there really is a retry counter,
assert re.match(r"Retrying in 3600 seconds \(\d+/\d+\)", comment)
Finally, you could wrap all of this into a helper with a predicate function for each key you want to check:
from itertools import partial
def verify_dict_contents(victim, matchers):
for key, matcher in matchers.items():
assert key in victim
assert matcher(victim[key])
return True
# ...
result = results.to_dict()
assert verify_dict_contents(result, {
"comment": partial(re.match, r"Retrying in 3600 seconds \(\d+/\d+\)"),
"number": lambda x: x >= 42
})