[Fixed]-Python: assert if string matches a format

25👍

You are looking for assertRegex():

class SomeTestCases(TestCase):
    def test_something(self):
        response = self.client.post(...)
        self.assertRegex(response.data, r'^your regex here$')

See also assertNotRegex.

👤mrts

8👍

Seems like regex would be the easiest solution here

import re
msg = 'Geometry type "point" is not supported'
assert re.match(r'^Geometry type ".+" is not supported$', msg)

Leave a comment