The other day I was troubled by Python Mock, so I will leave a memorandum.
[unittest.mock --- mock object library] (https://docs.python.org/ja/3/library/unittest.mock.html) [unittest.mock --- Introduction] (https://docs.python.org/ja/3/library/unittest.mock-examples.html)
It was officially written that I was worried about 2 hours. The formula is great
tori01.py
import requests
def get_qiita(url):
response = requests.get(url)
return response
Just get with url and return the returned response
test_tori01.py
import unittest
from mock import Mock, patch
import tori01
class TestTori01(unittest.TestCase):
@patch("tori01.requests.get")
def test_get_response(self, requests_get):
requests_get.return_value.status_code = 200
res = tori01.get_response("mock.url")
self.assertEqual(res.status_code, 200)
Assertion check with status code This time based on these codes
Since requests
is imported in tori01.py
Patched with @ patch ("tori01.requests.get")
If this is tori01.py
and requests.get
is imported
It will not be patched unless you do @ patch ("tori01.get ")
tori01.py
#Changed from import requests
from requests import get
def get_response(url):
response = get(url)
return response
test_tori01.py
import unittest
from mock import Mock, patch
import tori01
class TestTori01(unittest.TestCase):
# @patch("tori01.requests.get")Change from
@patch("tori01.get")
def test_get_response(self, requests_get):
requests_get.return_value.status_code = 200
res = tori01.get_response("mock.url")
self.assertEqual(res.status_code, 200)
test_tori01.py
import unittest
from mock import Mock, patch
import tori01
class TestTori01(unittest.TestCase):
# @patch("tori01.requests.get")Change from
#Argument naming is also requests.Change from get to requests
@patch("tori01.requests")
def test_get_response(self, requests):
# requests_get.return_value.status_code =Changed from 200
requests.get.return_value.status_code = 200
res = tori01.get_response("mock.url")
self.assertEqual(res.status_code, 200)
You can check the argument when it is called.
test_tori01.py
import unittest
from mock import Mock, patch
import tori01
class TestTori01(unittest.TestCase):
@patch("tori01.requests.get")
def test_get_response(self, requests_get):
#Create a function that returns a mock so that it will be called.
def requests_get_mock(url):
return Mock(status_code = 200, url = url)
requests_get.side_effect = requests_get_mock
res = tori01.get_response("mock.url")
self.assertEqual(res.status_code, 200)
self.assertEqual(res.url, "mock.url")
test_tori01.py
import unittest
from mock import Mock, patch
import tori01
class TestTori01(unittest.TestCase):
@patch("tori01.requests.get")
def test_get_response(self, requests_get):
requests_get.return_value.status_code = 200
res = tori01.get_response("mock.url")
self.assertEqual(res.status_code, 200)
requests_get.assert_called_with("mock.url") #add to
test_tori01.py
import unittest
from mock import Mock, patch
import tori01
class TestTori01(unittest.TestCase):
@patch("tori01.requests.get")
def test_get_response(self, requests_get):
vals = {"mock.url":200, "error_mock.url":404}
def requests_get_mock(val):
return Mock(status_code = vals[val])
requests_get.side_effect = requests_get_mock
res = tori01.get_response("mock.url")
res_error = tori01.get_response("error_mock.url")
self.assertEqual(res.status_code, 200)
self.assertEqual(res_error.status_code, 404)
test_tori01.py
import unittest
import requests
from mock import Mock, patch
import tori01
class TestTori01(unittest.TestCase):
@patch("tori01.requests.get")
def test_get_response(self, requests_get):
# side_If you set an exception class for effect, an exception will be thrown
requests_get.side_effect = requests.HTTPError
with self.assertRaises(requests.HTTPError):
tori01.get_response("mock.url")
Magic Mock ...
Recommended Posts