self.assertRegex
时间: 2024-01-16 10:03:59 浏览: 18
self.assertRegex is a Python method used in unit testing to check if a regular expression pattern matches a string. It is part of the unittest module in Python and is used to write test cases for functions and classes in a program.
The method takes two arguments: a regular expression pattern and a string. If the pattern matches the string, the test passes. If the pattern doesn't match the string, the test fails.
Example:
```
import unittest
class TestStringMethods(unittest.TestCase):
def test_regex(self):
pattern = r'^[a-z]+@[a-z]+\.[a-z]{2,3}$'
email = 'example@gmail.com'
self.assertRegex(email, pattern)
if __name__ == '__main__':
unittest.main()
```
In the above example, we have a test case that checks if an email address is in a valid format using a regular expression pattern. The assertRegex method checks if the email variable matches the pattern variable. If it does, the test passes. If it doesn't, the test fails.