Ok this is going to be a collection of notes to self.

This post does a good job of summing up some mock patterns with PyTest.

I then had to mock the Snyk client which depended on the Requests library. I chose not to mock the requests library but the methods in the snyk library.

The thing was the method I mocked returned the “Response” object and I wanted to use the json() feature of that object/class to return the results.

45 minutes later this worked.

    def test_get_week_of_scans(self, mocker):
        mocker.patch.object(SnykClient, 'post')
        response = Response()
        response.status_code = 200
        response.reason = "OK"
        response.encoding = "utf-8"
        results = json.dumps({
            "results": [
                {
                    "count": 1989
                }
            ]
        }).encode('utf-8')
        response._content = results
        SnykClient.post.return_value = response
        client = SnykReport()
        results = client.get_report_for_current_week(
            "98873-8ecc-46f0-b077-39322abc4390")
        assert results == 1989

I no longer hit their API and post returns the response I wanted.