1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] When writing unit tests in python, why are mocks in subsequent tests not overwriting...

Discussão em 'Python' iniciado por Stack, Outubro 4, 2024 às 06:22.

  1. Stack

    Stack Membro Participativo

    I am trying to write unit tests that involve mocking several libraries in different ways for each test. When I run each test individually, they all pass, but when I run them all together, many of them fail. The reason they fail is that when a method is mocked in two tests, the mock from the second test is ignored, and the mocked function always behaves as defined by the first test.

    I have made a demo project to demonstrate the issue:

    File Structure


    /.
    ├── src
    │ └── main.py
    └── test
    ├── __init__.py
    ├── test_base.py
    └── test.py

    Files


    main.py

    import boto3

    s3 = boto3.client('s3')

    def main():
    return s3.list_buckets()


    __init__.py

    from .test import *


    test_base.py

    import unittest
    from unittest.mock import MagicMock, patch

    class TestBase(unittest.TestCase):
    def setUp(self):
    self.patch_boto3_client = patch('boto3.client', autospec=True)

    self.mock_boto3_client = self.patch_boto3_client.start()

    # BOTO3

    # Set up mock boto3 clients
    self.mock_s3 = MagicMock()

    # Configure mock_boto3_client to return our mock clients
    self.mock_boto3_client.side_effect = lambda service: {
    's3': self.mock_s3,
    }[service]

    def tearDown(self) -> None:
    self.patch_boto3_client.stop()


    test.py

    from .test_base import TestBase

    class Test(TestBase):
    def test_1(self):
    from src.main import main

    self.mock_s3.list_buckets.return_value = 'test_1'

    response = main()

    self.assertEqual(response, 'test_1')


    def test_2(self):
    from src.main import main

    self.mock_s3.list_buckets.return_value = 'test_2'

    response = main()

    self.assertEqual(response, 'test_2')

    Results


    $ python3 -m coverage run -m unittest test
    .F
    ======================================================================
    FAIL: test_2 (test.test.Test.test_2)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
    File ".../test/test.py", line 21, in test_2
    self.assertEqual(response, 'test_2')
    AssertionError: 'test_1' != 'test_2'
    - test_1
    ? ^
    + test_2
    ? ^


    ----------------------------------------------------------------------
    Ran 2 tests in 0.292s

    FAILED (failures=1)


    I have tried patching by using decorators, and manually starting and stopping patch objects as shown in my code.

    How can I make my unit tests respect updates I make to mocked functions?

    Continue reading...

Compartilhe esta Página