断言字符串匹配pytest中的正则表达式
Asserting string matches regex in pytest
我正在使用 pytest 编写一个测试,以确保传入的时间戳字符串与适当的正则表达式格式匹配。我是通过以下方式完成的。
test_epoch():
timestamp = "1541811598.802"
epoch_regex = re.compile(r'^[0-9]+$')
assert epoch_regex.match(epoch)
但是,当测试运行时,出现以下错误:
AssertionError: assert None
+ where None = <built-in method match of re.Pattern object at 0x11ade6480>('1541840398.802')
+ where <built-in method match of re.Pattern object at 0x11ade6480> = re.compile('^[0-9]+$').match
有谁知道我哪里出错了以及如何正确断言字符串匹配正则表达式?
timestamp
真的匹配正则表达式吗?如果去掉 timestamp
中的 '.'
会怎样?我有预感 timestamp = "1541811598802"
会过去。
还要记住,'.'
是正则表达式中的一个特殊字符,因此当您修改正则表达式时,请务必考虑到这一点(提示,使用 \
转义特殊字符)!
我正在使用 pytest 编写一个测试,以确保传入的时间戳字符串与适当的正则表达式格式匹配。我是通过以下方式完成的。
test_epoch():
timestamp = "1541811598.802"
epoch_regex = re.compile(r'^[0-9]+$')
assert epoch_regex.match(epoch)
但是,当测试运行时,出现以下错误:
AssertionError: assert None
+ where None = <built-in method match of re.Pattern object at 0x11ade6480>('1541840398.802')
+ where <built-in method match of re.Pattern object at 0x11ade6480> = re.compile('^[0-9]+$').match
有谁知道我哪里出错了以及如何正确断言字符串匹配正则表达式?
timestamp
真的匹配正则表达式吗?如果去掉 timestamp
中的 '.'
会怎样?我有预感 timestamp = "1541811598802"
会过去。
还要记住,'.'
是正则表达式中的一个特殊字符,因此当您修改正则表达式时,请务必考虑到这一点(提示,使用 \
转义特殊字符)!