У меня есть сделал демонстрационный проект, чтобы продемонстрировать проблему:
Структура файла
Код: Выделить всё
/.
├── src
│ └── main.py
└── test
├── __init__.py
├── test_base.py
└── test.py
main.py
Код: Выделить всё
import boto3
s3 = boto3.client('s3')
def main():
return s3.list_buckets()
Код: Выделить всё
from .test import *
Код: Выделить всё
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()
Код: Выделить всё
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')
Код: Выделить всё
$ 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)
Как мне выполнить модульные тесты уважать обновления, которые я делаю для имитируемых функций?
Подробнее здесь: https://stackoverflow.com/questions/790 ... ot-overwri