pytest-mock mocker in pytest fixture

ehindy picture ehindy · Apr 12, 2017 · Viewed 17.2k times · Source

I'm trying to find out why I don't seem to be able to use a mocked return value in a fixture. With the following imports

import pytest
import uuid

pytest-mock example that works:

def test_mockers(mocker):
    mock_uuid = mocker.patch.object(uuid, 'uuid4', autospec=True)
    mock_uuid.return_value = uuid.UUID(hex='5ecd5827b6ef4067b5ac3ceac07dde9f')
    # this would return a different value if this wasn't the case
    assert uuid.uuid4().hex == '5ecd5827b6ef4067b5ac3ceac07dde9f'

The above test passes. However as I will be using this in many test cases I thought I could just use a fixture:

@pytest.fixture
def mocked_uuid(mocker):
    mock_uuid = mocker.patch.object(uuid, 'uuid4', autospec=True)
    mock_uuid.return_value = uuid.UUID(hex='5ecd5827b6ef4067b5ac3ceac07dde9f')
    return mock_uuid

def test_mockers(mocked_uuid):
    # this would return a different value if this wasn't the case
    assert uuid.uuid4().hex == '5ecd5827b6ef4067b5ac3ceac07dde9f'

The above fails with the following output:

FAILED 
phidgetrest\tests\test_taskscheduler_scheduler.py:62 (test_mockers)
mocked_uuid = <function uuid4 at 0x0000029738C5B2F0>

    def test_mockers(mocked_uuid):
        # this would return a different value if this wasn't the case
>       assert uuid.uuid4().hex == '5ecd5827b6ef4067b5ac3ceac07dde9f'
E       AssertionError: assert <MagicMock name='uuid4().hex' id='2848515660208'> == '5ecd5827b6ef4067b5ac3ceac07dde9f'
E        +  where <MagicMock name='uuid4().hex' id='2848515660208'> = <MagicMock name='uuid4()' id='2848515746896'>.hex
E        +    where <MagicMock name='uuid4()' id='2848515746896'> = <function uuid4 at 0x0000029738C5B2F0>()
E        +      where <function uuid4 at 0x0000029738C5B2F0> = uuid.uuid4

tests\test_taskscheduler_scheduler.py:65: AssertionError

Hoping someone can help me understand why one works and the other doesn't or even better provide a solution that works!

I've also tried changing the scope of the fixture[session, module, function], just in case as I don't really understand why it's failing.

Answer

ehindy picture ehindy · Apr 13, 2017

So found the culprit and it was really silly, I actually retyped the examples above rather then copy and paste so my original code has an issue. In my fixture I had typed:

mock_uuid.return_value(uuid.UUID(hex='5ecd5827b6ef4067b5ac3ceac07dde9f'))

When it should have been:

mock_uuid.return_value = uuid.UUID(hex='5ecd5827b6ef4067b5ac3ceac07dde9f')

which I had in my example, hence it was working for others...so many hours lost...Feeling rather silly, however I hope this may help someone in the future...