【发布时间】:2019-11-11 06:28:25
【问题描述】:
我有一个使用re.escape() 方法返回字符的函数。
在经验测试中它似乎有效,我想用 pytest 测试它。
但我无法让测试工作,所以经过几次尝试后,我尝试了类似的方法:
def test_escape():
> assert re.escape('!') == "\\!"
E AssertionError: assert '!' == '\\!'
E - !
E + \!
test/test_e.py:6: AssertionError
我还用解释器对其进行了测试,他的工作没有任何问题:
>>> re.escape('!') == '\\!'
True
使用“-s”禁用对pytest输出的捕获并尝试打印re.escape('!')的输出我得到"!"而不是"\!",这在解释器上不会发生。
我试图通过强制"\!" 作为输出来进行monkeypatch re.escape,它神奇地起作用。这显然不能解决我的问题,但通过 re.escape 突出了一些我不知道的问题
@pytest.fixture
def mock_escape(monkeypatch):
monkeypatch.setattr(re, "escape", lambda x: "\\!")
def test_escape(mock_escape):
assert re.escape('!') == "\\!"
...
test/test_e.py .
======================================== 1 passed in 0.07s =========================================
all test passed
出于好奇,我对原始函数做了同样的事情(没有猴子补丁,但编辑了它的返回),即使在这种情况下它也可以工作。 所以这不是因为导入而发生的问题。
# 编辑:#
正如 tmt 所发现的,这是 python 或 pytest 版本的问题。
python 3.7.2 和 pytest 5.2.1 出现问题。
python 3.6.3 和 pytest 4.5.0 不会出现此问题
所以几乎可以肯定这是一个错误(在我看来,pytest 更容易)
正如家伙回复的那样,这只是 re.escape() 的行为改变
【问题讨论】:
-
我在 pytest 中没有收到
assert re.escape("!") == "\\!"的断言错误。 (Python 3.6.7,pytest 5.2.2) -
是的!是真的!使用 python3.6 一切正常,我使用的是 3.7
标签: python regex escaping pytest