Deutsch   English   Français   Italiano  
<mailman.31.1725423711.2917.python-list@python.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail
From: Norman Robins <norman.robins59@gmail.com>
Newsgroups: comp.lang.python
Subject: Trouble with mocking
Date: Tue, 3 Sep 2024 17:34:13 -0700
Lines: 47
Message-ID: <mailman.31.1725423711.2917.python-list@python.org>
References: <CAF2QC-nP2Sd3y7O+TXV75=BwR0kXr8iRR-P-z6m8OWD5F_q4Ug@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de MhVSrZj1IxDn+ZR2ioyFCQLJrX2nxt+2aWGpWrv2ytVQ==
Cancel-Lock: sha1:Y/M6LfG3/fFZ15Nvx/Wn33Mqf2s= sha256:2JY/P8+4jsdxtYkuUlkx3LJGVSFoxVRNG1fpqnQWY2M=
Return-Path: <norman.robins59@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
 reason="2048-bit key; unprotected key"
 header.d=gmail.com header.i=@gmail.com header.b=Ky45jaJ/;
 dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.011
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'this:': 0.03; 'def': 0.04;
 'it"': 0.09; 'skip:\xc2 20': 0.09; 'import': 0.15; 'functions.':
 0.16; 'somewhat': 0.16; 'subject:Trouble': 0.16; 'yield': 0.16;
 'instead': 0.17; 'calls': 0.19; 'to:addr:python-list': 0.20;
 'code': 0.23; 'skip:p 30': 0.23; 'thanks!': 0.24; 'function':
 0.27; 'to:name:python': 0.32; 'message-id:@mail.gmail.com': 0.32;
 "i'm": 0.33; 'someone': 0.34; 'received:google.com': 0.34;
 'from:addr:gmail.com': 0.35; 'both': 0.40; 'want': 0.40; 'skip:m
 20': 0.63; 'let': 0.66; 'skip:t 20': 0.66; 'skip:\xc2 40': 0.69;
 'skip:f 20': 0.75; 'unit': 0.81
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1725410065; x=1726014865; darn=python.org;
 h=to:subject:message-id:date:from:mime-version:from:to:cc:subject
 :date:message-id:reply-to;
 bh=yTALGCdi3Gq1aNimVFiKAy0RdPCNrSOWKXu6RvUkp38=;
 b=Ky45jaJ/tcmids/Qy6+2Kwv79JDOYM3uUtTnc4In/2Ff+gbMDBvVXDbDGXCTn1wBO7
 OLBA3NqE+YdeqdVxRAbJuu0kUZQq6uMo7p4tI0Sn+WqtrPZ20lY9pTPrd0vHFNm1tqq1
 Rxb+7vY3mO1AN9wW+doBC7t8dXI7YYGGucVLKPgiD94+vZ/svEcyEqNLBe3r+gmKi6Fr
 dGVG/k3AHqQ2vm7EWhek53SQ5nCgf38Sv1F1Q//yBELj3m47flTTV91xKGBglfrfw7GU
 lC9+4EQZ17DflPhbjVLvCpIVj9pVwpIQhlzB+yyBiiAE9SIyRQ+76NvYKltBVVr/MhyF
 ztgQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1725410065; x=1726014865;
 h=to:subject:message-id:date:from:mime-version:x-gm-message-state
 :from:to:cc:subject:date:message-id:reply-to;
 bh=yTALGCdi3Gq1aNimVFiKAy0RdPCNrSOWKXu6RvUkp38=;
 b=VO4MhMrBKX7RBBFDR7C+0vpPu5PZx5GCOgCgGIjcS5gqwZAhiOH7Eu9j9xTB1IOmo+
 saKJybDu6ttTztPLWk+DpRbCq9Ku4hw3NsHZ5k51Mpuln9g13mrRmOPUosoe+P1h5mbM
 i47TTb1lH2ANCyYZqXn9aDOwSm4Lot5iyhnFuPqWIRbQn4eoPzRMBwk98Jp+lduFXAw7
 A+kI1msU2GF21C5zq5xTMri+OMw0Q3U23DdYU7dc+IXFzOunsQHSvwgiFRExO40KzMIj
 wm3zSeZ/+if2t3zAWfQGEIxhfmfrfbqBu7tTyp88ztreRs2MPuTO/Q5jGENPSI5F1cUr
 1s1g==
X-Gm-Message-State: AOJu0YypHxWS5BReLsbSiaGdcLDyu8/x1gCwlDLMZPkBahduQHTuDe1W
 cBbbZllfjDJLflgxnOuPCaykMBNcB3Q1z3Wf8qE+knB39plDVe5bBFnlC0SSI7/XV4Kb3YayA12
 BBtSc7PULfc62VMLUPsK+lNkV13HDW/02
X-Google-Smtp-Source: AGHT+IHGaxCEMZYjLDTn+TbzFCgpXNqecYCkFQkpW4s7LueYzzzcosVgYFP9MJ3Jx9FgXL4cIhwm2AdcukaSXTNR5UA=
X-Received: by 2002:a17:907:e8d:b0:a86:899d:6459 with SMTP id
 a640c23a62f3a-a8a32eda727mr171694866b.38.1725410064445; Tue, 03 Sep 2024
 17:34:24 -0700 (PDT)
X-Mailman-Approved-At: Wed, 04 Sep 2024 00:21:50 -0400
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
 <python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
 <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
 <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAF2QC-nP2Sd3y7O+TXV75=BwR0kXr8iRR-P-z6m8OWD5F_q4Ug@mail.gmail.com>
Bytes: 5338

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!