How to stub method in mocked object? - python

I need to test code snippet (e.g. from class UnderTestClass):
def _method_to_test(self)
...
ParsingObject = MyParsingClass()
if not ParsingObject.parse_string(some_string):
self.logger.error('Parsing data has failed.')
return False
return ParsingObject
No matter how I try, can't cover last return statement - return ParsingObject, so it have to be something wrong with my mocking of parse_string() method.
I've tried inter alia statements from Python testing tutorial:
from my_app import myParsingClass
...
def test_method_to_test_success(self):
...
UnderTestClassMock = Mock(name='UnderTestClass')
parsePatch = patch('my_app.myParsingClass.MyParsingClass')
parseMock = parsePatch.start()
parseInstance = parseMock.return_value
parseInstance.parse_string.return_value = True
res = tested_module.UnderTestClass._method_to_test(UnderTestClassMock)
parsePatch.stop()
self.assertIsInstance(res, myParsingClass.MyParsingClass)
But unfortunately get only:
AssertionError: False is not an instance of class 'my_app.myParsingClass.MyParsingClass'
UPDATE: Thanks. I follow your advice, so re-write a bit:
with patch('...') as ParseMock:
instance = ParseMock.return_value
ParseMock.parse_string.return_value = True
res = tested_module.UnderTestClass._method_to_test(UnderTestClassMock)
assert myParsingClass.MyParsingClass() is instance
assert myParsingClass.MyParsingClass() is res
but still got AssertionError on last line.
EDIT: do I need somehow dependency injection mechanism/framework?

You need to set return_value on parseMock, not parseInstance:
parseMock.parse_string.return_value = True
Also you need to stop() the patch after your assert

You should mock the instance method parse_string, not the class method.
In [22]: import mock
In [23]: ParseMock = mock.Mock()
In [24]: instance = ParseMock.return_value
In [25]: instance.parse_string.return_value = True
In [26]: parser = ParseMock()
In [27]: parser.parse_string("foo")
Out[27]: True

Related

Mock testing with nested function changes

I am adding testing to a pipeline project, code is already written and in production so it cannot be changed to accommodate the tests.
In simplest terms, if I have a function like so:
def other_foo():
return 1
def foo():
res = other_foo()
return res
In practicality, the other_foo call will return a variety of responses, but for testing, I want to create a fixed response to test foo.
So in my test I want to create a fixed response to other_foo of 2. and my test evaluation to be something like:
def test_foo():
# some mocking or nesting handle here for other_foo
res = foo()
assert res == 2
Use the patch decorator from unitest.mock and patch your module local variable.
from your.module import foo
from unitest.mock import patch
#patch('your.module.other_foo')
def test_foo(mock_other_foo):
mock_other_foo.return_value = 3
assert foo() == 3
mock_other_foo.return_value = 42
assert foo() == 42
You can find more information here and there.

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 instances
I tried to write the unit test,
def test_get_instance():
mocked_session: boto3.session.Session = boto3.session.Session()
def resource(service_name) -> boto3.resources.base.ServiceResource:
if service_name == 'ec2':
return MagicMock()
raise Exception('Parameter should be ec2')
mocked_session.resource = MagicMock(side_effect=resource) # resource is set?
mocked_session.resource('ec2').instances = MagicMock()
# mocked_session.resource('ec2').instances. ??? # not finished
filters = None
Node.get_nodes(mocked_session, filters)
assert session.resource('ec2').instances.filter.call_count == 1
However, the test function got the following error
> assert session.resource('ec2').instances.filter.call_count == 1
E AttributeError: 'function' object has no attribute 'resource'
You can use the built-in monckeypatch fixture for that purpose.
def test_get_instance(monkeypatch):
# ... your code
filter_calls = []
monkeypatch.setattr(mocked_session.resource('ec2'), 'filter', lambda Filters: filter_calls.append(1))
assert len(filter_calls) == 1
I did not test it with the boto3 lib, however, the idea is here.

mock patch multiple user inputs in sequence

similar questions have been asked many times, but I can't seem to figure out this simple test I am trying to build: I would like to first supply a "y", and then a "n" to a complex function requiring user input (i.e. it requires two inputs in sequence). This is my attempt - the with statement doesn't advance the iterator, but I don't know how I would implement patched input otherwise.
import mock
m = mock.Mock()
m.side_effect = ["y","n"]
#pytest.fixture(scope="module")
def test_my_complex_function():
with mock.patch('builtins.input', return_value=m()):
out = my_complex_function(some_args)
return out
If I understood well the problem, you have a fucntion that have a similar behavior like this.
module.py
def complex_function():
first = input("First input")
second = input("Second input")
return first, second
And you would to like to mock the input builtin method. You were in the right way, the only point to fix is that you have to build 2 mocks. One for each input instance.
test_module.py
import pytest
from unittest.mock import Mock, patch
from module import complex_function
input_mock_y = Mock() # First mock for first input call
input_mock_n = Mock() # Second mock for second input call
input_mock = Mock() # Combine the 2 mocks in another mock to patch the input call.
input_mock.side_effect = [input_mock_y.return_value, input_mock_n.return_value]
def test_my_complex_function():
with patch('builtins.input', input_mock) as mock_input:
result = complex_function()
assert mock_method.call_count == 2
You may say: Ok, but how do I know that each input was patched correctly?
So, you can especify some return value to any input mock, so you can compare.
input_mock_y = Mock()
input_mock_y.return_value = "Y"
input_mock_n = Mock()
input_mock_n.return_value = "N"
input_mock = Mock()
input_mock.side_effect = [input_mock_y.return_value, input_mock_n.return_value]
def test_my_complex_function():
with patch('builtins.input', input_mock) as mock_method:
result = function()
assert mock_method.call_count == 2
assert result == ('Y', 'N')

Mock nested method in Python

Wracking my brain on this. I want to mock generator methods self.api.redditor(username).comments.new(limit=num) and self.api.redditor(username).submissions.new(limit=num) below, in which self.api is assigned to a class instance, as in self.api = PrawReddit()
I'm trying to test the size of the result: self.assertEqual(len(result), 5)
So far, I tried MockPraw.return_value.redditor.return_value.comments.return_value.new.return_value.__iter__.return_value = iter(['c' * 10]) but the test fails with AssertionError: 0 != 5
Any tips much appreciated.
def get_comments_submissions(self, username, num=5):
"""Return max `num` of comments and submissions by `username`."""
coms = [
dict(
title=comment.link_title,
text=comment.body_html,
subreddit=comment.subreddit_name_prefixed,
url=comment.link_url,
created=datetime.fromtimestamp(comment.created_utc, pytz.utc),
)
for comment in self.api.redditor(username).comments.new(limit=num)
]
subs = [
dict(
title=submission.title,
text=submission.selftext_html,
subreddit=submission.subreddit_name_prefixed,
url=submission.url,
created=datetime.fromtimestamp(submission.created_utc, pytz.utc),
)
for submission in self.api.redditor(username).submissions.new(limit=num)
]
return coms + subs if len(coms + subs) < num else (coms + subs)[:num]
To mock a generator (unless you are using specific generator features) you can use an iterator as a stand-in eg
import unittest.mock as mock
generator_mock = Mock(return_value=iter(("foo", "bar")))
When you have nested structures like in your example this gets a little more complex, attribute access is automatically handled but return_value from a function must be defined. From your example:
# API mock
mock_api = Mock()
mock_api.redditor.return_value = mock_subs = Mock()
# Submissions mock
mock_subs.new.return_value = iter(("foo", "bar"))
This can then be called and asserted
for item in mock_api.api.redditor("user").submissions.new(limit=5):
print(item)
mock_api.redditor.assert_called_with("user")
mock_subs.new.assert_called_with(limit=5)
As the API is a member of the same class, this is going to have to be monkey patched eg:
target = Praw()
target.api = mock_api()
target.get_comments_submissions("user")
mock_api.redditor.assert_called_with("user")
mock_subs.new.assert_called_with(limit=5)
Note that the iterator in return value is a single instance and a second call to get the iterator will return the same instance.
Writting like you use pytest-mock and everything happens in mymodule (you imported the class at the top of the module like from xy import PrawReddit):
mocker.patch("datetime.fromtimestamp")
mocked_comment = mocker.MagicMock()
mocked_submission = mocker.MagicMock()
mocked = mocker.patch("mymodule.PrawReddit")
mocked.return_value.redditor.return_value.comments.new.return_value = [mocker.MagicMock(), mocked_comment]
mocked.return_value.redditor.return_value.submisions.new.return_value = [mocker.MagicMock(), mocked_submission]
returned = instance.get_comments_submissions("foo", num=2)
assert mocked.return_value.redditor.call_count = 2
mocked.return_value.assert_called_with("foo")
assert returned[-1]["link_title"] == mocked_comment.link_title
Another test call with the same intro:
# ...
returned = instance.get_comments_submissions("foo")
assert mocked.return_value.redditor.call_count = 2
mocked.return_value.assert_called_with("foo")
assert returned[1]["link_title"] == mocked_comment.link_title
assert returned[-1]["title"] == mocked_submission.title

How to mock a function with specific parameter and return value that calls another function in python unit test?

i am new to python unit testing. Want to mock a function that calls other functions.
Here is my function that i want to mock
def has_groups(self, group_names):
auth_user_id = AuthUser.get_by_email(self.userEmail).id
auth_user_groups = AuthUserGroups.get_group_by_user_id(auth_user_id)
for auth_user_group in auth_user_groups:
if auth_user_group.group.name in group_names:
return True
return False
has_groups should return True only when it get's 'Admin' as parameter.
Here is my test
def my_test(self):
uid = self.auth_user.get_by_email = Mock(return_value=73)
groups = AuthUserGroups.get_group_by_user_id = Mock(uid, return_value='Admin')
self.auth_user.has_groups = Mock(groups, return_value=True)
but it's not working fine. I will appreciate if anyone help me
Can i use patch decorator for this and how?
As I understand your has_groups function is method. I think it's better to mock whole class or independent function. On this situation you could mock AuthUserGroups, method return value and patch module with has_groups method implementation. So you'll have test like this:
from unittest import mock
def my_test(self):
group = mock.MagicMock()
group.group.name = 'Admin'
fake_auth_user_groups = mock.MagicMock()
fake_auth_user_groups.get_group_by_user_id.return_value = [group]
with mock.patch('your_module.AuthUserGroups', fake_auth_user_groups):
self.auth_user.has_groups(['Admin'])

Categories

Resources