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


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.



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