Questions tagged [python-mock]

A test utility that patches objects with user-defined code or data.

Mock is a Python tool for unit testing. It can patch some functions or objects with mock code with some specified properties like return values. It records and provides information on how the mock object is called.

470 questions
0
votes
1 answer

Python mock/unittest: How do you mock a class member?

I am trying to unit test a script that reads a requirements.txt file and goes through each line installing it with pip. However it my unit test it doesn't seem to enter the loop because the self.requirements is empty. I tried setting it to…
Xaztek
  • 15
  • 4
0
votes
1 answer

How to replace a method with an arbitrary function inside test function (not using patch.object)?

My attempt was to mock the database operation inside a function with a predefined value. I patched my mongo collection instance's find method and set a list of dictionary as a demo return value (find return more or less in similar data structure).…
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
0
votes
1 answer

Test if python's builtin __mul__ was called using mock

I have a method where I am converting dollars to cents before calling an api. # money.py def send_money(dollars): cents = dollars * 100 return send_money_to_api(cents) * This is not exactly my code. But this is sufficient for…
Hussain
  • 5,057
  • 6
  • 45
  • 71
0
votes
1 answer

How to mock.patch a dependancy in Python?

Mocking a method that is a dependancy is not working for me. When the method I need to test is calling its dependancy method, the real method is called instead of the mock version of it. I do have the following files: myLibrary.py from…
Pablo
  • 68
  • 1
  • 6
0
votes
0 answers

Changing the arguments to Open() using python Mock

I am trying to change the arguments to open() so that my test code will open the files in the test/ directory instead of the original one. file.py def testing_opening(): f = open( 'file', 'r' ) return f.read() Now, in my test file, I want…
0
votes
1 answer

How to mock a function imported directly by the tested module without knowing the module name in python

Assume I have a function defined in a module: module_a.py def foo(): return 10 And I want to create an API to patch the function: patcher.py import mock class Patcher(object): def __enter__(self): self.patcher =…
snudler6
  • 59
  • 9
0
votes
1 answer

Mocking Files In Python Unittest In Imported Modules

I readily admit to going a bit overboard with unit testing. While I have passing tests, I find my solution to be inelegant, and I'm curious if anyone has a cleaner solution. The class being tested: class Config(): def __init__(self): …
Jordon Birk
  • 480
  • 1
  • 9
  • 28
0
votes
1 answer

Best practices using python mock for testing functions within sub modules

So, consider I have a simple library that I am trying to write unit-tests for. This library talks to a database and then uses that data to call an SOAP API. I have three modules, and a testfile for each module. dir structure: ./mypkg …
amulllb
  • 3,036
  • 7
  • 50
  • 87
0
votes
1 answer

Python Unittest; How to get the parameters passed when the function is called?

I am trying to make a unit test to check if this python function (dispatch) passes the correct parameters to deal_with_result. Is there a way to "hijack" the input parameters when the function deal_with_result is called in dispatch? I do not have…
0
votes
1 answer

Mock testing try/except with print statements

I am currently writing a unit test for my Python2 script. I am having trouble writing a test case that catches both the sys.exit and the print statement to validate the exception. Any help would be appreciated. except ParseError, e: if…
0
votes
1 answer

Configure return value mock with decorator only

Is there a way to capture the following logic in the patch decorator instead of having to pass the mock into the function: @patch('boto3.client') def test_playing_with_saml(self, boto3_client): boto3_client.return_value.assume_role_with_saml =…
RNikoopour
  • 523
  • 4
  • 16
0
votes
2 answers

Nested spec_set in mock

I have two following files: testcase_module.py import boto3 ec2 = boto3.resource('ec2') def f(): return ec2.instances.all() testcase_test.py import testcase_module import unittest.mock class MainTest(unittest.TestCase): …
pt12lol
  • 2,332
  • 1
  • 22
  • 48
0
votes
1 answer

Python mock return default value after initial predefined values returned

I'm mocking a message queue, where I want it to return 2 messages and then return None each time it is called (to simulate that the message queue is now empty). However, after the 3rd message call I get an error from mocking framework saying that…
Craig
  • 2,286
  • 3
  • 24
  • 37
0
votes
1 answer

How to patch a class in python unit test and get a handle on patched object's return value

I am testing a class's method in Python 2.7 using Mock 2.0.0 Library Here is how the method under test looks like: from sklearn.externals import joblib class ClassUnderTest(): def MethodUnderTest(self, path, myDict): …
Hary
  • 1,127
  • 4
  • 24
  • 51
0
votes
1 answer

Assert is function not passed in parameter is called

Let's say I have a function like that: def foo(): bar() Is there any way I could test if bar is called, when I'm not passing bar as parameter? My team use Python 3.6 and 3.5
Ginko
  • 385
  • 1
  • 15