I checked how to use pytest-mock, so make a note Since it is troublesome to check the use cases that are likely to be used in the future, I summarized them. It's an article I wrote in a day after I checked pytest, so I think there are various excesses and deficiencies, but if I find a better way, I will update it as needed.
It is one of the libraries used when implementing python test code. Please refer to the official website for details.
pytest https://docs.pytest.org/en/latest/index.html
pytest-mock https://pypi.org/project/pytest-mock/
The following hoge method is the test target.
from mailer import Mailer
#example messages
# ['1st text', 'Second text']
def hoge(messages):
try:
#Send as many emails as there are messages
for message in messages:
has_error = Mailer.send(message)
if has_error:
#1 is returned when an error occurs
return 1
#Returns 0 at the end of normal
return 0
except Exception as e:
#2 is returned when an exception occurs
return 2
The version used at the time of writing this article is as follows.
Mailer.send
with a mocktest_hoge.py
from hoge import hoge
from mailer import Mailer
from unittest.mock import call
def test_hoge(mocker):
# Mailer.Replace send with mock
mailer_mock = mocker.patch.object(Mailer, 'send')
messages = ['1st text', 'Second text']
#Mailer above.Since send is a mock, even if you execute hoge, it is a real Mailer.send will not be executed
assert hoge(messages) == 0
test_hoge.py
from hoge import hoge
from mailer import Mailer
from unittest.mock import call
def test_hoge(mocker):
# Mailer.Replace send with mock
mailer_mock = mocker.patch.object(Mailer, 'send')
messages = ['1st text', 'Second text']
assert hoge(messages) == 0
# Mailer.You can see that send is called twice
assert mailer_mock.call_count == 2
test_hoge.py
from hoge import hoge
from mailer import Mailer
from unittest.mock import call
def test_hoge(mocker):
# Mailer.Replace send with mock
mailer_mock = mocker.patch.object(Mailer, 'send')
#Since messages are empty, Mailer. prevent send from being called
messages = []
assert hoge(messages) == 0
# Mailer.Verify that send is not called
mailer_mock.assert_not_called()
test_hoge.py
from hoge import hoge
from mailer import Mailer
from unittest.mock import call
def test_hoge(mocker):
# Mailer.Replace send with mock
mailer_mock = mocker.patch.object(Mailer, 'send')
messages = ['1st text', 'Second text']
assert hoge(messages) == 0
#The first time'1st text', The second time'Second text'Can be verified that is being passed and called as a parameter
mailer_mock.assert_has_calls([call('1st text'), call('Second text')])
test_hoge.py
from hoge import hoge
from mailer import Mailer
from unittest.mock import call
def test_hoge(mocker):
# side_Pass a list of return values as an array to effect
#Each time it is called, it will be returned in order from the beginning of the array
#In this example, False is returned the first time and True is returned the second time.
mailer_mock = mocker.patch.object(Mailer, 'send', side_effect = [False, True])
messages = ['1st text', 'Second text']
#Second Mailer.Since send returns True, you can verify the return value 1.
assert hoge(messages) == 1
test_hoge.py
from hoge import hoge
from mailer import Mailer
from unittest.mock import call
def test_hoge(mocker):
# side_Specify an exception for effect
#In this example, the first time is False and the second time is an exception.
mailer_mock = mocker.patch.object(Mailer, 'send', side_effect = [False, Exception('error')])
messages = ['1st text', 'Second text']
#Second Mailer.Since send raises an exception, the return value 2 can be verified.
assert hoge(messages) == 2
Recommended Posts