[PYTHON] How to write urlfetch unittest on GAE / P

Copy below

http://stackoverflow.com/questions/9943996/how-to-mock-ndb-get-context-urlfetch

import unittest
from google.appengine.ext import testbed, ndb
from mock import patch, Mock

class MyTestCase(unittest.TestCase):

    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_urlfetch_stub()

        # mock urlrfetch service
        uf = self.testbed.get_stub('urlfetch')
        uf._Dynamic_Fetch = Mock()

    @patch('google.appengine.api.urlfetch.urlfetch_service_pb.URLFetchResponse')
    def test_make_request(self, URLFetchResponse):
        # mocking rpc response object
        response = URLFetchResponse.return_value
        response.contentwastruncated.return_value = False
        response.statuscode.return_value = 200
        response.content.return_value = 'Hello world!'        

        ctx = ndb.get_context()
        fut = ctx.urlfetch('http://google.com')
        result = fut.get_result()

        self.assertEquals(result.content, 'Hello world!')

    def tearDown(self):
        self.testbed.deactivate()

Recommended Posts

How to write urlfetch unittest on GAE / P
Notes on how to write requirements.txt
I don't know how to get query parameters on GAE / P
To write to Error Repoting in Python on GAE
XPath Basics (2) -How to write XPath
How to register on pypi
[ROS] How to write Publisher and Subscriber on one node
How to write a Python class
How to install mysql-connector-python on mac
How to write soberly in pandas
How to use Dataiku on Windows
Flask reuse How to write html
Notes on how to use pywinauto
How to install graph-tool on macOS
How to install VMware-Tools on Linux
How to install pycrypto on Windows
How to deploy django-compressor on Windows
Notes on how to use featuretools
How to install OpenCV on Mac
How to run matplotlib on heroku
How to install PyPy on CentOS
How to use homebrew on Debian
Misunderstanding on how to connect cnn
How to install TensorFlow on CentOS 7
Commands often used on GAE / P
How to write Docker base image
How to write Django1.9 environment-independent wsgi.py
Notes on how to use doctest
How to install Maven on CentOS
How to install Go on Ubuntu
How to install music 21 on windows
How to write code to access python dashDB on Bluemix or local
Difference in execution speed depending on how to write Cython function
A story about wanting to think about garbled characters on GAE / P
How to install aws-session-manager-plugin on Manajro Linux
Qiita (1) How to write a code name
How to read pydoc on python interpreter
How to install drobertadams / toggl-cli on Mac
How to set optuna (how to write search space)
[Kivy] How to install Kivy on Windows [Python]
How to use mecab, neologd-ipadic on colab
How to write Python document comments (Docstrings)
How to build Hello, World on #Nix
How to update php on Amazon linux 2
How to use Google Assistant on Windows 10
How to erase Python 2.x on Mac.
How to display emoji on Manjaro Linux
Memorandum on how to use gremlin python
How to install packages on Alpine Linux
[Python2.7] Summary of how to use unittest
How to install Anisble on Amazon Linux 2
How to switch mouse operations on CentOS
How to install richzhang / colorization on Windows 10
How to update security on CentOS Linux 8
How to install Apache (httpd) on CentOS7
How to install php7.4 on Linux (Ubuntu)
How to install Eclipse GlassFish 5.1.0 on CentOS 7
How to install Apache (httpd) on CentOS8
How to test on a Django-authenticated page
How to run Cython on OSX Memo
How to find large files on Linux