开发者

Equivalent of assertRegexMatches in python 2.4

Say I have a regex

REGEX = re.compile('.*foo{')

How would you write a unit test that matches a set of string with python 2.4 ?

I know in pyth开发者_StackOverflow社区on 2.7 I can use assertRegexMatches, unfortunately this doesn't work in 2.4 :/

I use self.assertEqual for the rest of my tests.

Cheers, M


self.assertTrue(REGEX.match(text))


Since you asked about a set of string rather than a single string

def createMatcher( self, regex ):
    def matchCheck( argument ):
        self.assertTrue( regex.match( argument ) )
    return matchCheck

Then in your function:

map( self.createMatcher( REGEX ), mySetOfStrings )


If you want an exact match you can do this:

assertTrue(REGEX.match(data))

If you do not care where it matches then:

assertTrue(REGEX.search(data))

Keep in mind the difference between matching and searching. Also if you are so inclined you can subclass TestCase and add your own assertion to do the above.


For testing on earlier Python versions, I prefer to use unittest2: http://pypi.python.org/pypi/unittest2/

It's a backport of unittest maintained by Michael Foord, the same developer that maintains the stdlib version.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜