當前位置: 首頁>>代碼示例>>Python>>正文


Python monkeypatch.MonkeyPatch方法代碼示例

本文整理匯總了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 
開發者ID:eth-brownie,項目名稱:brownie,代碼行數:21,代碼來源:conftest.py

示例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 
開發者ID:pdbpp,項目名稱:pdbpp,代碼行數:20,代碼來源:conftest.py

示例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()),
    ] 
開發者ID:pgjones,項目名稱:hypercorn,代碼行數:23,代碼來源:test_h11.py

示例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 
開發者ID:pgjones,項目名稱:hypercorn,代碼行數:19,代碼來源:test_config.py

示例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 
開發者ID:LarsMichelsen,項目名稱:pmatic,代碼行數:21,代碼來源:lib.py

示例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] 
開發者ID:pytest-dev,項目名稱:pytest,代碼行數:27,代碼來源:test_monkeypatch.py

示例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 
開發者ID:pytest-dev,項目名稱:pytest,代碼行數:20,代碼來源:test_monkeypatch.py

示例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 
開發者ID:pytest-dev,項目名稱:pytest,代碼行數:18,代碼來源:test_monkeypatch.py

示例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] 
開發者ID:pytest-dev,項目名稱:pytest,代碼行數:21,代碼來源:test_monkeypatch.py

示例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 
開發者ID:pytest-dev,項目名稱:pytest,代碼行數:27,代碼來源:test_monkeypatch.py

示例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 
開發者ID:pgjones,項目名稱:quart,代碼行數:9,代碼來源:test_cli.py

示例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") 
開發者ID:pgjones,項目名稱:quart,代碼行數:4,代碼來源:test_cli.py

示例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") 
開發者ID:pgjones,項目名稱:quart,代碼行數:4,代碼來源:test_cli.py

示例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") 
開發者ID:pgjones,項目名稱:quart,代碼行數:4,代碼來源:test_cli.py

示例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() 
開發者ID:pgjones,項目名稱:quart,代碼行數:12,代碼來源:test_cli.py


注:本文中的_pytest.monkeypatch.MonkeyPatch方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。