And*_*ndi 3 monkeypatching pytest hypothesis-test python-hypothesis
在单元测试中,我使用monkeypatch来更改dict.
from hypothesis import given, strategies
test_dict = {"first": "text1", "second": "text2"}
given(val=strategies.text())
def test_monkeypath(monkeypatch, val):
monkeypatch.setitem(test_dict, "second", val)
assert isinstance(test_dict["second"], str)
Run Code Online (Sandbox Code Playgroud)
测试通过了,但在使用 执行以下测试代码时收到警告pytest。
=================================================================================================================== warnings summary ====================================================================================================================
.PyCharm2019.2/config/scratches/hypothesis_monkeypatch.py::test_monkeypath
c:\users\d292498\appdata\local\conda\conda\envs\pybt\lib\site-packages\hypothesis\extra\pytestplugin.py:172: HypothesisDeprecationWarning: .PyCharm2019.2/config/scratches/hypothesis_monkeypatch.py::test_monkeypath uses the 'monkeypatch' fixture, wh
ich is reset between function calls but not between test cases generated by `@given(...)`. You can change it to a module- or session-scoped fixture if it is safe to reuse; if not we recommend using a context manager inside your test function. See h
ttps://docs.pytest.org/en/latest/fixture.html#sharing-test-data for details on fixture scope.
note_deprecation(
-- Docs: https://docs.pytest.org/en/stable/warnings.html
============================================================================================================= 1 passed, 1 warning in 0.30s ==============================================================================================================
Run Code Online (Sandbox Code Playgroud)
这是否意味着无论生成多少测试用例,字典的值只会更改一次hypothesis?
我不确定在这种情况下如何使用上下文管理器。有人可以指出我正确的方向吗?
@given(val=strategies.text())
def test_monkeypath(monkeypatch, val):
with monkeypatch.context() as m:
m.setitem(test_dict, "second", val)
assert isinstance(test_dict["second"], str)
Run Code Online (Sandbox Code Playgroud)
您的问题是,对于所有测试调用,该字典仅修补一次,并且假设正在警告您这一点。如果你在这句话之前有任何逻辑monkeypatch.setitem,那就太糟糕了!
您可以直接使用 Monkeypatch来解决这个问题,而不是通过固定装置:
from hypothesis import given, strategies
from _pytest.monkeypatch import MonkeyPatch
test_dict = {"first": "text1", "second": "text2"}
@given(val=strategies.text())
def test_monkeypath(val):
assert test_dict["second"] == "text2" # this would fail in your version
with MonkeyPatch().context() as mp:
mp.setitem(test_dict, "second", val)
assert test_dict["second"] == val
assert test_dict["second"] == "text2"
Run Code Online (Sandbox Code Playgroud)
等等,没有警告。
| 归档时间: |
|
| 查看次数: |
1288 次 |
| 最近记录: |