本文整理汇总了Python中_pytest.monkeypatch.MonkeyPatch方法的典型用法代码示例。如果您正苦于以下问题:Python monkeypatch.MonkeyPatch方法的具体用法?Python monkeypatch.MonkeyPatch怎么用?Python monkeypatch.MonkeyPatch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类_pytest.monkeypatch
的用法示例。
在下文中一共展示了monkeypatch.MonkeyPatch方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pytest_sessionstart
# 需要导入模块: from _pytest import monkeypatch [as 别名]
# 或者: from _pytest.monkeypatch import MonkeyPatch [as 别名]
def pytest_sessionstart():
monkeypatch_session = MonkeyPatch()
monkeypatch_session.setattr(
"solcx.get_available_solc_versions",
lambda: [
"v0.6.7",
"v0.6.2",
"v0.5.15",
"v0.5.8",
"v0.5.7",
"v0.5.0",
"v0.4.25",
"v0.4.24",
"v0.4.22",
],
)
# worker ID for xdist process, as an integer
示例2: _tmphome
# 需要导入模块: from _pytest import monkeypatch [as 别名]
# 或者: from _pytest.monkeypatch import MonkeyPatch [as 别名]
def _tmphome(tmpdir_factory):
"""Set up HOME in a temporary directory.
This ignores any real ~/.pdbrc.py then, and seems to be
required also with linecache on py27, where it would read contents from
~/.pdbrc?!.
"""
from _pytest.monkeypatch import MonkeyPatch
mp = MonkeyPatch()
tmphome = tmpdir_factory.mktemp("tmphome")
mp.setenv("HOME", str(tmphome))
mp.setenv("USERPROFILE", str(tmphome))
with tmphome.as_cwd():
yield tmphome
示例3: test_protocol_handle_max_incomplete
# 需要导入模块: from _pytest import monkeypatch [as 别名]
# 或者: from _pytest.monkeypatch import MonkeyPatch [as 别名]
def test_protocol_handle_max_incomplete(monkeypatch: MonkeyPatch) -> None:
config = Config()
config.h11_max_incomplete_size = 5
MockHTTPStream = AsyncMock() # noqa: N806
MockHTTPStream.return_value = AsyncMock(spec=HTTPStream)
monkeypatch.setattr(hypercorn.protocol.h11, "HTTPStream", MockHTTPStream)
context = Mock()
context.event_class.return_value = AsyncMock(spec=IOEvent)
protocol = H11Protocol(AsyncMock(), config, context, False, None, None, AsyncMock())
await protocol.handle(RawData(data=b"GET / HTTP/1.1\r\nHost: hypercorn\r\n"))
protocol.send.assert_called()
assert protocol.send.call_args_list == [
call(
RawData(
data=b"HTTP/1.1 400 \r\ncontent-length: 0\r\nconnection: close\r\n"
b"date: Thu, 01 Jan 1970 01:23:20 GMT\r\nserver: hypercorn-h11\r\n\r\n"
)
),
call(RawData(data=b"")),
call(Closed()),
]
示例4: test_create_sockets_ip
# 需要导入模块: from _pytest import monkeypatch [as 别名]
# 或者: from _pytest.monkeypatch import MonkeyPatch [as 别名]
def test_create_sockets_ip(
bind: str,
expected_family: socket.AddressFamily,
expected_binding: Tuple[str, int],
monkeypatch: MonkeyPatch,
) -> None:
mock_socket = Mock()
monkeypatch.setattr(socket, "socket", mock_socket)
config = Config()
config.bind = [bind]
sockets = config.create_sockets()
sock = sockets.insecure_sockets[0]
mock_socket.assert_called_with(expected_family, socket.SOCK_STREAM)
sock.setsockopt.assert_called_with(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # type: ignore
sock.bind.assert_called_with(expected_binding) # type: ignore
sock.setblocking.assert_called_with(False) # type: ignore
sock.set_inheritable.assert_called_with(True) # type: ignore
示例5: API
# 需要导入模块: from _pytest import monkeypatch [as 别名]
# 或者: from _pytest.monkeypatch import MonkeyPatch [as 别名]
def API(self, request):
self._monkeypatch = monkeypatch()
if not is_testing_with_real_ccu():
# First hook into urlopen to fake the HTTP responses
self._monkeypatch.setattr(pmatic.api, 'urlopen', fake_urlopen)
else:
# When executed with real ccu we wrap urlopen for enabling recording
self._monkeypatch.setattr(pmatic.api, 'urlopen', wrap_urlopen)
API = pmatic.api.RemoteAPI(
address="http://192.168.1.26",
credentials=("Admin", "EPIC-SECRET-PW"),
connect_timeout=5,
#log_level=pmatic.DEBUG,
)
request.addfinalizer(API.close)
return API
示例6: test_setattr
# 需要导入模块: from _pytest import monkeypatch [as 别名]
# 或者: from _pytest.monkeypatch import MonkeyPatch [as 别名]
def test_setattr() -> None:
class A:
x = 1
monkeypatch = MonkeyPatch()
pytest.raises(AttributeError, monkeypatch.setattr, A, "notexists", 2)
monkeypatch.setattr(A, "y", 2, raising=False)
assert A.y == 2 # type: ignore
monkeypatch.undo()
assert not hasattr(A, "y")
monkeypatch = MonkeyPatch()
monkeypatch.setattr(A, "x", 2)
assert A.x == 2
monkeypatch.setattr(A, "x", 3)
assert A.x == 3
monkeypatch.undo()
assert A.x == 1
A.x = 5
monkeypatch.undo() # double-undo makes no modification
assert A.x == 5
with pytest.raises(TypeError):
monkeypatch.setattr(A, "y") # type: ignore[call-overload]
示例7: test_delattr
# 需要导入模块: from _pytest import monkeypatch [as 别名]
# 或者: from _pytest.monkeypatch import MonkeyPatch [as 别名]
def test_delattr() -> None:
class A:
x = 1
monkeypatch = MonkeyPatch()
monkeypatch.delattr(A, "x")
assert not hasattr(A, "x")
monkeypatch.undo()
assert A.x == 1
monkeypatch = MonkeyPatch()
monkeypatch.delattr(A, "x")
pytest.raises(AttributeError, monkeypatch.delattr, A, "y")
monkeypatch.delattr(A, "y", raising=False)
monkeypatch.setattr(A, "x", 5, raising=False)
assert A.x == 5
monkeypatch.undo()
assert A.x == 1
示例8: test_setitem
# 需要导入模块: from _pytest import monkeypatch [as 别名]
# 或者: from _pytest.monkeypatch import MonkeyPatch [as 别名]
def test_setitem() -> None:
d = {"x": 1}
monkeypatch = MonkeyPatch()
monkeypatch.setitem(d, "x", 2)
monkeypatch.setitem(d, "y", 1700)
monkeypatch.setitem(d, "y", 1700)
assert d["x"] == 2
assert d["y"] == 1700
monkeypatch.setitem(d, "x", 3)
assert d["x"] == 3
monkeypatch.undo()
assert d["x"] == 1
assert "y" not in d
d["x"] = 5
monkeypatch.undo()
assert d["x"] == 5
示例9: test_delenv
# 需要导入模块: from _pytest import monkeypatch [as 别名]
# 或者: from _pytest.monkeypatch import MonkeyPatch [as 别名]
def test_delenv() -> None:
name = "xyz1234"
assert name not in os.environ
monkeypatch = MonkeyPatch()
pytest.raises(KeyError, monkeypatch.delenv, name, raising=True)
monkeypatch.delenv(name, raising=False)
monkeypatch.undo()
os.environ[name] = "1"
try:
monkeypatch = MonkeyPatch()
monkeypatch.delenv(name)
assert name not in os.environ
monkeypatch.setenv(name, "3")
assert os.environ[name] == "3"
monkeypatch.undo()
assert os.environ[name] == "1"
finally:
if name in os.environ:
del os.environ[name]
示例10: test_undo_class_descriptors_delattr
# 需要导入模块: from _pytest import monkeypatch [as 别名]
# 或者: from _pytest.monkeypatch import MonkeyPatch [as 别名]
def test_undo_class_descriptors_delattr() -> None:
class SampleParent:
@classmethod
def hello(_cls):
pass
@staticmethod
def world():
pass
class SampleChild(SampleParent):
pass
monkeypatch = MonkeyPatch()
original_hello = SampleChild.hello
original_world = SampleChild.world
monkeypatch.delattr(SampleParent, "hello")
monkeypatch.delattr(SampleParent, "world")
assert getattr(SampleParent, "hello", None) is None
assert getattr(SampleParent, "world", None) is None
monkeypatch.undo()
assert original_hello == SampleChild.hello
assert original_world == SampleChild.world
示例11: loadable_app
# 需要导入模块: from _pytest import monkeypatch [as 别名]
# 或者: from _pytest.monkeypatch import MonkeyPatch [as 别名]
def loadable_app(monkeypatch: MonkeyPatch) -> Mock:
app = Mock(spec=Quart)
app.cli = AppGroup()
module = Mock()
module.app = app
monkeypatch.setattr(quart.cli, "import_module", lambda _: module)
return app
示例12: dev_env_patch
# 需要导入模块: from _pytest import monkeypatch [as 别名]
# 或者: from _pytest.monkeypatch import MonkeyPatch [as 别名]
def dev_env_patch(monkeypatch: MonkeyPatch) -> None:
monkeypatch.setenv("QUART_ENV", "development")
示例13: debug_env_patch
# 需要导入模块: from _pytest import monkeypatch [as 别名]
# 或者: from _pytest.monkeypatch import MonkeyPatch [as 别名]
def debug_env_patch(monkeypatch: MonkeyPatch) -> None:
monkeypatch.setenv("QUART_DEBUG", "true")
示例14: no_debug_env_patch
# 需要导入模块: from _pytest import monkeypatch [as 别名]
# 或者: from _pytest.monkeypatch import MonkeyPatch [as 别名]
def no_debug_env_patch(monkeypatch: MonkeyPatch) -> None:
monkeypatch.setenv("QUART_DEBUG", "false")
示例15: test_shell_command
# 需要导入模块: from _pytest import monkeypatch [as 别名]
# 或者: from _pytest.monkeypatch import MonkeyPatch [as 别名]
def test_shell_command(app: Mock, monkeypatch: MonkeyPatch) -> None:
runner = CliRunner()
interact = Mock()
monkeypatch.setattr(code, "interact", interact)
app.make_shell_context.return_value = {}
app.import_name = "test"
os.environ["QUART_APP"] = "module:app"
runner.invoke(cli, ["shell"])
app.make_shell_context.assert_called_once()
interact.assert_called_once()