Questions tagged [magicmock]

MagicMock is a subclass of Mock with default implementations of most of the magic methods.

MagicMock is a subclass of Mock with default implementations of most of the magic methods.

You can use MagicMock without having to configure the magic methods yourself.

163 questions
0
votes
0 answers

Mocking method from another class with python unittest mock

I have a class and within that class there are used methods from another classes. Let's say it looks like that (just an example): from jobs.fruit import Fruit from jobs.veggie import Veggie class Healthy: def start(self, arg): …
0
votes
1 answer

Python MagicMock returns bytes value for HTTP response

I want to mock HTTP request that returns an image (bytes) with MagicMock. So, I have simple function here: import io import urllib.request def blah(): try: req = urllib.request.Request(url='', data=None, method='GET') …
0
votes
0 answers

What is the best way to perform unit testing with socket module based code and mocking

I created a Player class that is used to subscribe to a game server as well as responding to pings and applying the AI's algorithm moves. The class works as intended but I am trying to test the functions within it and I am having issues. Here is my…
LEBaz2211
  • 1
  • 1
0
votes
1 answer

Python3 unit tests for API

I could use some much needed guidance on unit tests for python3 All my files are in the same directory. No Sub folders. Params is a class that gets initialized in the script. FILE 1 works fine connecting to the API. #FILE 1 AssetsSensu.py import…
0
votes
0 answers

Python mock not applied to object under test

I am attempting to mock an object to perform some testing test.py @patch("api.configuration.client.get_configuration") def test(mock_client_get_configuration): mock_client_get_configuration.return_value = "123" result = code_under_test() …
Remotec
  • 10,304
  • 25
  • 105
  • 147
0
votes
1 answer

How to magic mock long chained calls?

To test the following functino, I want to assert .filter() is called once with parameter filters. def get_instances(session: boto3.session.Session, filters): instances = session.resource('ec2').instances.filter(Filters=filters) return…
ca9163d9
  • 27,283
  • 64
  • 210
  • 413
0
votes
0 answers

Mocked values mismatch with MagicMock

I am working on a python project, where we read parquet files from azure datalake and perform the required operations. We have defined a common parquet file reader function which we use to read such files. I am using MagicMock to mock objects and…
Kashyap
  • 385
  • 3
  • 13
0
votes
1 answer

Python Unit Test Mock import & check assert called with

I have a class ProductionClass with a method method_to_test which I want to test. Class ProductionClass has dependency to api, which I want to mock in the test. from my_module.apis import api class ProductionClass: def method_to_test: …
Philip Frerk
  • 91
  • 1
  • 1
  • 11
0
votes
0 answers

Mocking psycopg2 connection to test if data was inserted

class Test_Postgres(unittest.TestCase): def setUp(self): #mock connection conx = Mock() #mock cursor cursor = MagicMock() cursor.__enter__.return_value = MagicMock() cursor.__exit__ =…
0
votes
1 answer

How to create magic mock for simple if statement checking if an item exists in a list

I am trying to test my method that simply checks if a letter is in either of the lists of letters. If it is in list a, then I assign myletter to an imported object called yes; Otherwise, I assign myletter to an imported object called no. I tried to…
r_e
  • 242
  • 4
  • 14
0
votes
1 answer

Mocking cursor.fetchone() returns None istead of returning a value in python

I wrote a function for my project which fetches data from MSSQL server using Pyodbc. The function works fine. When I write unittest cases using unittest and mock library and mocked the cursor.fetchone and returned a predefined value but while…
0
votes
1 answer

python mocks a function properly, however the call count is not accounted

I have a class like the below: Class a: def fn1(self): p1=multiprocessing.Process(self.fn2, (arg1,) p1.start() p1.join() def fn2(self, arg1): … I am trying to test it and I have mocked the self.fn2…
0
votes
1 answer

Mocking dependencies

I am Python newbie and trying to understand how to mock methods in UTs. Here is my Test test_module2.py from module2 import A import unittest from unittest.mock import patch, MagicMock class TestBulkLoad(unittest.TestCase): …
Michael Z
  • 3,883
  • 10
  • 43
  • 57
0
votes
1 answer

How can I verify a Python method was called with parameters including a DataFrames?

I'm using MagicMock to verify that a method was called with specific arguments: ClassToMock.method_to_mock =…
algorhythm
  • 3,304
  • 6
  • 36
  • 56
0
votes
1 answer

MagicMock not called for signal handler in Python 3

I was in the middle of migrating some code and associated tests from python 2 to python 3 (specifically 2.7 to 3.7) and I came across this weird behavior that I'd like to understand: try: from unittest import mock # python 3 except…
Kevin
  • 6,993
  • 1
  • 15
  • 24