I have a unit test that asserts that a certain regex pattern appears in a multiline string:
import unittest
class TestStrRegex(unittest.TestCase):
def test_pattern_matches_thrice(self):
string = """\
repeatedval=78
badval=89
# comment
repeatedval=20
repeatedval=8978
"""
self.assertRegex(string, 'repeatedval=\d+')
I would like to assert that the 'repeatedval=\d+'
pattern matches exactly three times in the string, something like this:
self.assertRegex(string, 'repeatedval=\d+', exact_number_of_matches=3)
But that doesn't work. Is there another way to make this assertion?
Use the re
module directly, and assert that the result has a particular length.
def test_pattern_matches_thrice(self):
string = """..."""
result = re.findall(r'repeatedval=\d+', string)
self.assertEqual(len(result), 3)