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
1 answer

MagicMock return value based on input

I am trying to refactor my tests from flexmock to mock. Given the following syntax from flexmock: flexmock(subprocess).should_receive('check_output').with_args('ls /').and_return(output) How can I rewrite this using Mock? In particular, how do I…
Jon Skarpeteig
  • 4,118
  • 7
  • 34
  • 53
0
votes
1 answer

Mocking Numpy Structured Arrays

I'm trying to figure out how to mock a numpy structured array and am not having much luck. Ideally, I'd like to do something like this: from mock import MagicMock mock_obj = MagicMock() mock_obj['some']['test']['structure'] = 3 assert 3 == …
user985030
  • 1,557
  • 1
  • 16
  • 32
0
votes
1 answer

Testing my object sends a command method to another object

I'm trying to make sure Testme.command() calls bar() on an instance of Dependency but I keep coming up short. I'm running this with python -m unittest tests.test_config and this code lives in tests/test_config.py in my project. class Dependency(): …
0
votes
1 answer

Why does Order matter in Kwarg parameters in MagicMock asserts?

I have a test where I am mocking a filter call on a manager. the assert looks like this: filter_mock.assert_called_once_with(type_id__in=[3, 4, 5, 6], finance=mock_finance, parent_transaction__date_posted=tran_date_posted) and the code being tested…
Nathan Tregillus
  • 6,006
  • 3
  • 52
  • 91
0
votes
1 answer

@mock.patch isn't raising an attribute error even after setting side_effect

I'm attempting to fix a bug in the python package caniusepython3 which arises because distlib isn't parsing pypi projects correctly. I've written this unit test @mock.patch('distlib.locators.locate') def…
AlexLordThorsen
  • 8,057
  • 5
  • 48
  • 103
0
votes
1 answer

Mocking and patching keystoneclient in Python unittest

I am writing unittest for a class looks like below. I am trying to assert if logging is properly called with patch keystoneclient. The class looks like below. Problem is, I cannot pass through the for statement and can never get to LOGGER.warning or…
James C.
  • 501
  • 2
  • 7
  • 16
0
votes
1 answer

Python Mock Delay side_effect valuation

Is there any way to delay the evaluation of a Mock side_effect. def mockfunc(wheel): wheel.rubber = soft wheel.make = Mock(side_effect=mockfunc(wheel))) The problem here is that I'm trying to replace the 'wheel.make' method such that when it…
evolution
  • 4,332
  • 5
  • 29
  • 34
0
votes
0 answers

Substituting a Python class method for a mock method

I have a question similar to the questions posted here, which also have no satisfying answer yet: mocking a method with another reusable method with arguments in python Using mock patch to mock an instance method I have the following Production…
physicalattraction
  • 6,485
  • 10
  • 63
  • 122
0
votes
1 answer

Python: issues understanding magicmock with unittests

Here is my class: class WorkflowsCloudant(cloudant.Account): def __init__(self, account_id): super(WorkflowsCloudant, self).__init__(settings.COUCH_DB_ACCOUNT_NAME, …
dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189
-1
votes
1 answer

Unable to mock instance attribute while leveraging side_effect

I am trying to perform something similar to this: mymock = Mock() mymock.variable.side_effect = [1,2,3] print mymock.variable # Should print 1 print mymock.variable # Should print 2 print mymock.variable # Should print 3 However, I am not getting…
Voldemort
  • 181
  • 1
  • 9
-1
votes
1 answer

Python magic mock not setting the mocked value

I am writing python tests for the first time. I am trying to test a basic mock. I want to return some value that I want when I call the function, rather than a mock object. Here is the code: In views: def myfunction(): return "Actual data" In…
sinshil
  • 11
  • 4
-1
votes
1 answer

Python's magic mock is not working as expected. Actual function gets called

I have a model: class Evaluation(models.Model): def abc(self): #some logic here return something Say, I have another function which uses this: def filter_graded(evaluations): return filter(lambda x: x.is_completely_graded(),…
inertia
  • 404
  • 4
  • 15
-2
votes
1 answer

How to improve readabilty and maintainability of @patch and MagicMock statements (avoid long names and String identification)?

In my test code I have a lot of boilerplate expressions "Magic", "return_". I also have lengthy strings to identify the paths of the functions to mock. The strings are not automatically replaced during refactoring and I would prefer to directly use…
Stefan
  • 10,010
  • 7
  • 61
  • 117
1 2 3
10
11