Skip to content

@mock.patch decorator doesnt work properly with @pytest.mark.asyncio #42

@sashgorokhov

Description

@sashgorokhov

Sample test code:

tests.py

from unittest import mock

import pytest
import testmodule


@pytest.mark.asyncio
async def test_async_mock_patch_contextmanager():
    with mock.patch('testmodule.foo') as foo_mock:
        assert testmodule.foo is foo_mock


@pytest.mark.asyncio
@mock.patch('testmodule.foo')
async def test_async_mock_patch_decorator(foo_mock):
    assert testmodule.foo is foo_mock  # FAILS


def test_mock_patch_contextmanager():
    with mock.patch('testmodule.foo') as foo_mock:
        assert testmodule.foo is foo_mock


@mock.patch('testmodule.foo')
def test_mock_patch_decorator(foo_mock):
    assert testmodule.foo is foo_mock

testmodule.py

def foo():
    return 'Bar'

test_async_mock_patch_decorator will fail, because testmodule.foo is <function foo at ###>, not <MagicMock name='foo' id='###'>

Tested this on python3.6 only.

UPDATE:

Seems that on python 3.5.2 there is no such bug. All tests succeeded.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions