【发布时间】:2020-12-28 18:35:01
【问题描述】:
在单元测试中,我使用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)
测试通过,但在使用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 ==============================================================================================================
这是否意味着无论hypothesis生成多少个测试用例,dict的值都只会改变一次?
在这种情况下,我不确定如何使用上下文管理器。有人可以指出我正确的方向吗?
【问题讨论】:
标签: pytest monkeypatching hypothesis-test python-hypothesis