コード例 #1
0
ファイル: test_pytest_mock.py プロジェクト: kujyp/pytest-mock
def test_assert_called_with_unicode_arguments(mocker: MockerFixture) -> None:
    """Test bug in assert_call_with called with non-ascii unicode string (#91)"""
    stub = mocker.stub()
    stub(b"l\xc3\xb6k".decode("UTF-8"))

    with pytest.raises(AssertionError):
        stub.assert_called_with("lak")
コード例 #2
0
ファイル: test_pytest_mock.py プロジェクト: kujyp/pytest-mock
def test_assert_called_once_with_wrapper(mocker: MockerFixture) -> None:
    stub = mocker.stub()
    stub("foo")
    stub.assert_called_once_with("foo")
    stub("foo")
    with assert_traceback():
        stub.assert_called_once_with("foo")
コード例 #3
0
ファイル: test_pytest_mock.py プロジェクト: kujyp/pytest-mock
def test_assert_any_call_wrapper(mocker: MockerFixture) -> None:
    stub = mocker.stub()
    stub("foo")
    stub("foo")
    stub.assert_any_call("foo")
    with assert_traceback():
        stub.assert_any_call("bar")
コード例 #4
0
ファイル: test_pytest_mock.py プロジェクト: kujyp/pytest-mock
def test_assert_called_once_wrapper(mocker: MockerFixture) -> None:
    stub = mocker.stub()
    if not hasattr(stub, "assert_called_once"):
        pytest.skip("assert_called_once not available")
    stub("foo")
    stub.assert_called_once()
    stub("foo")
    with assert_traceback():
        stub.assert_called_once()
コード例 #5
0
    async def test_context_manager(self,
                                   mocker: pytest_mock.MockerFixture) -> None:
        async with servo.DurationProgress("5ms") as progress:
            stub = mocker.stub()
            async for update in progress.every("0.1ms"):
                stub(update.progress)

            stub.assert_called()
            assert progress.progress == 100.0
コード例 #6
0
 def __test_failure_message(self, mocker: MockerFixture, **kwargs: Any) -> None:
     expected_name = kwargs.get("name") or "mock"
     if NEW_FORMATTING:
         msg = "expected call not found.\nExpected: {0}()\nActual: not called."
     else:
         msg = "Expected call: {0}()\nNot called"
     expected_message = msg.format(expected_name)
     stub = mocker.stub(**kwargs)
     with pytest.raises(AssertionError) as exc_info:
         stub.assert_called_with()
     assert str(exc_info.value) == expected_message
コード例 #7
0
ファイル: test_pytest_mock.py プロジェクト: kujyp/pytest-mock
def test_assert_called_args_with_introspection(mocker: MockerFixture) -> None:
    stub = mocker.stub()

    complex_args = ("a", 1, {"test"})
    wrong_args = ("b", 2, {"jest"})

    stub(*complex_args)
    stub.assert_called_with(*complex_args)
    stub.assert_called_once_with(*complex_args)

    with assert_argument_introspection(complex_args, wrong_args):
        stub.assert_called_with(*wrong_args)
        stub.assert_called_once_with(*wrong_args)
コード例 #8
0
def test_assert_called_kwargs_with_introspection(mocker: MockerFixture) -> None:
    stub = mocker.stub()

    complex_kwargs = dict(foo={"bar": 1, "baz": "spam"})
    wrong_kwargs = dict(foo={"goo": 1, "baz": "bran"})

    stub(**complex_kwargs)
    stub.assert_called_with(**complex_kwargs)
    stub.assert_called_once_with(**complex_kwargs)

    with assert_argument_introspection(complex_kwargs, wrong_kwargs):
        stub.assert_called_with(**wrong_kwargs)
        stub.assert_called_once_with(**wrong_kwargs)
コード例 #9
0
ファイル: test_pytest_mock.py プロジェクト: kujyp/pytest-mock
def test_monkeypatch_ini(testdir: Any, mocker: MockerFixture) -> None:
    # Make sure the following function actually tests something
    stub = mocker.stub()
    assert stub.assert_called_with.__module__ != stub.__module__

    testdir.makepyfile("""
        import py.code
        def test_foo(mocker):
            stub = mocker.stub()
            assert stub.assert_called_with.__module__ == stub.__module__
    """)
    testdir.makeini("""
        [pytest]
        mock_traceback_monkeypatch = false
    """)
    result = testdir.runpytest_subprocess()
    assert result.ret == 0
コード例 #10
0
ファイル: test_pytest_mock.py プロジェクト: kujyp/pytest-mock
def test_assert_has_calls(mocker: MockerFixture) -> None:
    stub = mocker.stub()
    stub("foo")
    stub.assert_has_calls([mocker.call("foo")])
    with assert_traceback():
        stub.assert_has_calls([mocker.call("bar")])
コード例 #11
0
ファイル: test_pytest_mock.py プロジェクト: kujyp/pytest-mock
def test_assert_not_called_wrapper(mocker: MockerFixture) -> None:
    stub = mocker.stub()
    stub.assert_not_called()
    stub()
    with assert_traceback():
        stub.assert_not_called()
コード例 #12
0
ファイル: test_pytest_mock.py プロジェクト: kujyp/pytest-mock
 def test_repr_with_name(self, mocker: MockerFixture) -> None:
     test_name = "funny walk"
     stub = mocker.stub(name=test_name)
     assert "name={!r}".format(test_name) in repr(stub)
コード例 #13
0
ファイル: test_pytest_mock.py プロジェクト: kujyp/pytest-mock
 def test_repr_with_no_name(self, mocker: MockerFixture) -> None:
     stub = mocker.stub()
     assert "name" not in repr(stub)
コード例 #14
0
ファイル: test_pytest_mock.py プロジェクト: kujyp/pytest-mock
 def test_call(self, mocker: MockerFixture) -> None:
     stub = mocker.stub()
     stub("foo", "bar")
     stub.assert_called_once_with("foo", "bar")