Skip to content

Instantly share code, notes, and snippets.

@rhoboro
Created November 22, 2017 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhoboro/a0e7be93e0244b67709c1daa50a06c13 to your computer and use it in GitHub Desktop.
Save rhoboro/a0e7be93e0244b67709c1daa50a06c13 to your computer and use it in GitHub Desktop.
GAE/SE/pyでfetchurlを使うメソッドのテスト
import unittest
import json
from mock import Mock, patch
from google.appengine.ext import testbed
class FetchTestCase(unittest.TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_urlfetch_stub()
uf = self.testbed.get_stub('urlfetch')
uf._Dynamic_Fetch = Mock()
def tearDown(self):
self.testbed.deactivate()
class TestSomeAPI(FetchTestCase):
@patch(
'google.appengine.api.urlfetch.urlfetch_service_pb.URLFetchResponse')
def test_some_api(self, URLFetchResponse):
response = URLFetchResponse.return_value
response.contentwastruncated.return_value = False
response.statuscode.return_value = 200
expected = {"result": 1}
response.content.return_value = json.dumps(expected)
with patch.dict('os.environ', {'API_URL': 'localhost/'}):
results = some_api("hoge", 1234, "01-001", 2980)
self.assertEquals(results, expected_some_api_return_value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment