python-mock

Accessing Calls to Mocked Class functions

Accessing Calls to Mocked Class functions Question: I have written a custom class to mock a general API client in a codebase so that I can centrally and easily mock all of the class methods for unit testing. This is working great so far, however I am looking for a way to track individual calls …

Total answers: 1

Python mock function that returns dictionary multiple times

Python mock function that returns dictionary multiple times Question: Background I have a function my_func that gets a dictionary from another function get_dict and modifies it. On failure, my_func gets retried until it succeeds or called a specified number of times. What complicates this is that the dictionary must have exactly one key-value pair, otherwise …

Total answers: 1

Mocking os.path.exists and os.makedirs returning AssertionError

Mocking os.path.exists and os.makedirs returning AssertionError Question: I have a function like below. # in retrieve_data.py import os def create_output_csv_file_path_and_name(output_folder=’outputs’) -> str: """ Creates an output folder in the project root if it doesn’t already exist. Then returns the path and name of the output CSV file, which will be used to write the data. …

Total answers: 1

Mocking in Behave and Flask

Mocking in Behave and Flask Question: I am trying to mock an HTTP request call using mock because I don’t want to Behave to call it actually. So I have this code scenario in matches.py file: import request def get_match(): response = request.get(“https://example.com”) return response And in my step definition match_steps.py for behave I have …

Total answers: 1

how to test boto3 resource download file raising 404 error using mock?

how to test boto3 resource download file raising 404 error using mock? Question: I want to test s3 resource download_file Here is the code I want to test def logfile_downloader(): s3 = boto3.resource(‘s3′) bucket = s3.Bucket(bucket) for object in bucket.objects.filter(Prefix=’logs/access_2018’): try: bucket.download_file(object.key, ‘logs/’ + save_path + ‘/’ + object.key.split(‘/’)[-1]) except botocore.exceptions.ClientError as e: if e.response[‘Error’][‘Code’] …

Total answers: 1

How to throw exception from mocked instance's method?

How to throw exception from mocked instance's method? Question: This demo function I want to test is pretty straight forward. def is_email_deliverable(email): try: return external.verify(email) except Exception: logger.error(“External failed failed”) return False This function uses an external service which I want to mock out. But I can’t figure out how to throw an exception from …

Total answers: 2

Mock Patches Appearing in the Wrong Order?

Mock Patches Appearing in the Wrong Order? Question: I have a test module (test.py) which imports functions from another module (keyboard.py). keyboard.py def get_keys(keyList, timeStamped): return event.getKeys(keyList=keyList, timeStamped=timeStamped) def wait_keys(keyList, timeStamped): return event.waitKeys(keyList=keyList, timeStamped=timeStamped) test.py @mock.patch(‘keyboard.wait_keys’) @mock.patch(‘keyboard.get_keys’) def test_2(self, mock_waitKeys, mock_getKeys): mock_waitKeys.return_value = [[‘wait_keys!’, 0.1]] mock_getKeys.return_value = [[‘get_keys!’,0.1]] run_blocks(trials,noise,win,expInfo, incorrect, tone1, tone2, experiment_details,allPoints,32,60) I’m trying …

Total answers: 1

How to Mock a user input in Python

How to Mock a user input in Python Question: I am currently trying to learn how to Unit Test with Python and was introduced to the concept of Mocking, I am a beginner Python developer hoping to learn the concepts of TDD alongside my development of Python skills. I am struggling to learn the concept …

Total answers: 3

How do I mock part of a python constructor just for testing?

How do I mock part of a python constructor just for testing? Question: I am new to Python, so I apologize if this is a duplicate or overly simple question. I have written a coordinator class that calls two other classes that use the kafka-python library to send/read data from Kafka. I want to write …

Total answers: 1

How to mock a property

How to mock a property Question: I’m asking how to mock a class property in a unit test using Python 3. I’ve tried the following, which makes sense for me following the docs, but it doesn’t work: foo.py: class Foo(): @property def bar(self): return ‘foobar’ def test_foo_bar(mocker): foo = Foo() mocker.patch.object(foo, ‘bar’, new_callable=mocker.PropertyMock) print(foo.bar) I’ve …

Total answers: 2