Skip to content

Instantly share code, notes, and snippets.

@kevinhowbrook
Last active March 21, 2020 20:15
Show Gist options
  • Save kevinhowbrook/4056496631c4ac5b03c242a89634edfa to your computer and use it in GitHub Desktop.
Save kevinhowbrook/4056496631c4ac5b03c242a89634edfa to your computer and use it in GitHub Desktop.
Python Django mock request eg

How to test a class method using mock:

You can patch the entire requests module with:

@mock.patch('requests.get')

But it's better to keep it specific as per:

@mock.patch('rca.shortcourses.access_planit.requests.get')
    """ Patch the request module totally to force the timeout so we can test the
        result of the try/expect.

        Test here that data returned from a timeout is a empty dict
    """
    @mock.patch('rca.shortcourses.access_planit.requests.get')
    def test_data_if_timeout(self, mock_get):
        #logging.disable(logging.CRITICAL)  # Don't log exceptions... is this bad?
        mock_get.side_effect = Timeout
        data = AccessPlanitXML(course_id=1)
        xml_data = data.get_data()
        self.assertEqual(xml_data, {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment