Trouble with mocking

Liste des GroupesRevenir à cl python 
Sujet : Trouble with mocking
De : norman.robins59 (at) *nospam* gmail.com (Norman Robins)
Groupes : comp.lang.python
Date : 04. Sep 2024, 02:34:13
Autres entêtes
Message-ID : <mailman.31.1725423711.2917.python-list@python.org>
References : 1
I'm somewhat new to mocking for unit tests.

I have some code like this:

In foo/bar/baz.py I have 2 function I want to mock, one calls the other"
def function1_to_mock():
   .
   .
   .

def function2_to_mock():
   function1_to_mock()

In foo/bar/main.py I import 1 of these and call it"
from .baz import function2_to_mock

def some_function():
    function1_to_mock()
    .
    .
    .

I want to mock both function1_to_mock and function2_to_mock

In my test I do this:

def function1_to_mock(kid):
    return MOCKED_VALUE

@pytest.fixture(autouse=True)
def mock_dependencies():
    with patch(foo.bar.baz.function1_to_mock') as mock_function1_to_mock, \
         patch('foo.bar.main.function2_to_mock') as mock_function2_to_mock:
        mock_function2_to_mock.return_value = {
            'this': 'that
        }
        yield mock_function1_to_mock, mock_function2_to_mock

def test_main(mock_dependencies):
    some_function()

When some_function is called the real function1_to_mock is called instead
of my mock.

Can someone please let me know how to properly mock these 2 functions.

Thanks!

Date Sujet#  Auteur
4 Sep 24 * Trouble with mocking2Norman Robins
20 Sep 24 `- Re: Trouble with mocking1Mark Bourne

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal