Pytest mocked function takes too long to run

I'm learning to use mocking while writing unit tests. But my tests always take the time it would take if I was not mocking them. Consider this example function:

application.py

from time import sleep
    
def is_windows():
    # fake slow operation
    sleep(5)
    return True


def get_operating_system():
    return 'Windows' if is_windows() else 'Linux'

application_test.py

from application import get_operating_system


# 'mocker' fixture provided by pytest-mock
def test_get_operating_system(mocker):
    # Mock the slow function to return True
    mocker.patch('application.is_windows', return_value=True)
    assert get_operating_system() == 'Windows'

For some reason this test always takes more than 5 seconds to run. If I remove sleep(5) from is_windows() then the test runs in under a second, so it looks like the function isn't being mocked at all! Am I doing something wrong here? Any help appreciated. Thanks.

EDIT: I'm running my tests from PyCharm.


You need to import application if you want to mock something in it.

import application

def test_get_operating_system(mocker):
    # Mock the slow function to return True
    mocker.patch('application.is_windows', return_value=True)
    assert application.get_operating_system() == 'Windows'

Otherwise, you aren't using the patch.


edit: Actually, maybe I'm crazy about that. You code mocks it as expected. I have run it on my machine and it works correctly.

outputs

no patch:

1 passed in 5.03s

with mocker.patch("application.is_windows", return_value=True):

1 passed in 0.02s

with mocker.patch("application.is_windows", return_value=False):

1 failed in 0.10s