Category : unit-testing

Theres some class # logging.py class Logging: def debug(msg): print(msg) def error(msg): print(msg) # index.py from logging import Logging def run(): self.logger = Logging() logger.debug(‘hello world’) case 1 : I wanna mock debug function to ignore print. how to mock up logging class? test1 is mockup debug function. it works fine test2 is mockup whole ..

Read more

I’d like to get all archives from a specific directory on S3 bucket like the following: def get_files_from_s3(bucket_name, s3_prefix): files = [] s3_resource = boto3.resource("s3") bucket = s3_resource.Bucket(bucket_name) response = bucket.objects.filter(Prefix=s3_prefix) for obj in response: if obj.key.endswidth(‘.zip’): # get all archives files.append(obj.key) return files My question is about testing it; because I’d like to mock ..

Read more

I am trying to create a decorator that will run my pytest test twice, once regularly, and then secondly with my service’s processor being replaced by the fixture using patch. However, I am having issues with it. @pytest.fixture def new_processor(): return ServiceProcessor( config=some_other_fixture.config ) def patch_service_processor(processor): return patch.object(MyService, MyService._get_processor.__name__, return_value=processor) def run_with_old_and_new(new_processor): def deco(func): def ..

Read more

A_script.py from uuid import uuid4 def get_unique_identifier(env, customer_id): return env + ‘-‘ + customer_id + ‘-‘ + str(uuid4())[0:8] test_A_script.py import unittest from unittest.mock import patch import src.A_script as a_script class MyTestCase(unittest.TestCase): @patch(‘uuid.uuid4’) def test_get_unique_identifier(self, mock_uuid4): mock_uuid4.return_value = ‘abcd1234’ expected = ‘test_env-test_cust-abcd1234’ unique_identifier = a_script.get_unique_identifier(‘test_env’, ‘test_cust’) self.assertEqual(expected, unique_identifier) How can I make uuid4 return ‘abcd1234’? Source: ..

Read more