使用py.test在Python中测试正则表达式

Rya*_*n M 4 python unit-testing pytest python-3.x

对我来说,正则表达式仍然是一种黑暗艺术,但我认为这只是需要练习的事情之一.因此,我更关心的是能够生成py.test函数,向我展示我的正则表达式失败的地方.我目前的代码是这样的:

my_regex = re.compile("<this is where the magic (doesn't)? happen(s)?>")

def test_my_regex():
    tests = ["an easy test that I'm sure will pass",
             "a few things that may trip me up",
             "a really pathological, contrived example",
             "something from the real world?"]

    test_matches = [my_regex.match(test) for test in tests]

    for i in range(len(tests)):
        print("{}: {!r}".format(i, tests[i]))
        assert test_matches[i] is not None
Run Code Online (Sandbox Code Playgroud)

我运行时的输出py.test myfile.py是这样的

0: "an easy..."
1: "a few things..."
2: "a really pathological..."
Run Code Online (Sandbox Code Playgroud)

最后一个是第一个(仅?)没有通过测试的那个.

我想我可以做一些像

assertSequenceEqual(test_matches, [not None]*len(test_matches))
Run Code Online (Sandbox Code Playgroud)

但这似乎很严重,而且我认为这<object> is not None是检查对象不是None而不是的首选方式<object> != None.

Bru*_*ira 16

另一种方法是使用参数化.

my_regex = re.compile("<this is where the magic (doesn't)? happen(s)?>")

@pytest.mark.parametrize('test_str', [
    "an easy test that I'm sure will pass",
    "a few things that may trip me up",
    "a really pathological, contrived example",
    "something from the real world?",
])
def test_my_regex(test_str):
     assert my_regex.match(test_str) is not None
Run Code Online (Sandbox Code Playgroud)

这将为每个测试字符串生成一个独立的测试用例.这种IMO更清洁,更容易添加新案例,并且还具有允许每个test_str人单独失败而不影响其他人的优势.