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


Python monkeypatch.monkeypatch類代碼示例

本文整理匯總了Python中_pytest.monkeypatch.monkeypatch的典型用法代碼示例。如果您正苦於以下問題:Python monkeypatch類的具體用法?Python monkeypatch怎麽用?Python monkeypatch使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了monkeypatch類的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_issue1338_name_resolving

def test_issue1338_name_resolving():
    pytest.importorskip('requests')
    monkeypatch = MonkeyPatch()
    try:
         monkeypatch.delattr('requests.sessions.Session.request')
    finally:
        monkeypatch.undo()
開發者ID:6br,項目名稱:servo,代碼行數:7,代碼來源:test_monkeypatch.py

示例2: test_setenv

def test_setenv():
    monkeypatch = MonkeyPatch()
    monkeypatch.setenv('XYZ123', 2)
    import os
    assert os.environ['XYZ123'] == "2"
    monkeypatch.undo()
    assert 'XYZ123' not in os.environ
開發者ID:6br,項目名稱:servo,代碼行數:7,代碼來源:test_monkeypatch.py

示例3: test_setitem_deleted_meanwhile

def test_setitem_deleted_meanwhile():
    d = {}
    monkeypatch = MonkeyPatch()
    monkeypatch.setitem(d, 'x', 2)
    del d['x']
    monkeypatch.undo()
    assert not d
開發者ID:6br,項目名稱:servo,代碼行數:7,代碼來源:test_monkeypatch.py

示例4: test_issue156_undo_staticmethod

def test_issue156_undo_staticmethod(Sample):
    monkeypatch = MonkeyPatch()

    monkeypatch.setattr(Sample, 'hello', None)
    assert Sample.hello is None

    monkeypatch.undo()
    assert Sample.hello()
開發者ID:6br,項目名稱:servo,代碼行數:8,代碼來源:test_monkeypatch.py

示例5: test_setenv_deleted_meanwhile

def test_setenv_deleted_meanwhile(before):
    key = "qwpeoip123"
    if before:
        os.environ[key] = "world"
    monkeypatch = MonkeyPatch()
    monkeypatch.setenv(key, 'hello')
    del os.environ[key]
    monkeypatch.undo()
    if before:
        assert os.environ[key] == "world"
        del os.environ[key]
    else:
        assert key not in os.environ
開發者ID:6br,項目名稱:servo,代碼行數:13,代碼來源:test_monkeypatch.py

示例6: test_setenv_prepend

def test_setenv_prepend():
    import os
    monkeypatch = MonkeyPatch()
    monkeypatch.setenv('XYZ123', 2, prepend="-")
    assert os.environ['XYZ123'] == "2"
    monkeypatch.setenv('XYZ123', 3, prepend="-")
    assert os.environ['XYZ123'] == "3-2"
    monkeypatch.undo()
    assert 'XYZ123' not in os.environ
開發者ID:6br,項目名稱:servo,代碼行數:9,代碼來源:test_monkeypatch.py

示例7: test_delattr

def test_delattr():
    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:6br,項目名稱:servo,代碼行數:18,代碼來源:test_monkeypatch.py

示例8: test_setattr

def test_setattr():
    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
    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
開發者ID:6br,項目名稱:servo,代碼行數:22,代碼來源:test_monkeypatch.py

示例9: test_delenv

def test_delenv():
    name = 'xyz1234'
    assert name not in os.environ
    monkeypatch = MonkeyPatch()
    pytest.raises(KeyError, "monkeypatch.delenv(%r, raising=True)" % name)
    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:6br,項目名稱:servo,代碼行數:19,代碼來源:test_monkeypatch.py

示例10: test_delitem

def test_delitem():
    d = {'x': 1}
    monkeypatch = MonkeyPatch()
    monkeypatch.delitem(d, 'x')
    assert 'x' not in d
    monkeypatch.delitem(d, 'y', raising=False)
    pytest.raises(KeyError, "monkeypatch.delitem(d, 'y')")
    assert not d
    monkeypatch.setitem(d, 'y', 1700)
    assert d['y'] == 1700
    d['hello'] = 'world'
    monkeypatch.setitem(d, 'x', 1500)
    assert d['x'] == 1500
    monkeypatch.undo()
    assert d == {'hello': 'world', 'x': 1}
開發者ID:6br,項目名稱:servo,代碼行數:15,代碼來源:test_monkeypatch.py

示例11: test_setitem

def test_setitem():
    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:6br,項目名稱:servo,代碼行數:16,代碼來源:test_monkeypatch.py


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