简体   繁体   中英

Python mock REST API

I need to do unittest for a method that has 2 rest calls and another method call. To describe in details, it is as below:

class A():
    def methA(self, param1):
        val1 = methB(someParam)
        res1 = requests.get(url1, headers=headers, verify=False)
        res2 = requests.post(endpoint, payload, headers=headers, verify=False)
        val2 = methC(someParam)
        return val2

    def methB(self, param1):
        res = requests.get(url, headers=headers, verify=False)
        return res

    def methC(self, param1):
        res = requests.get(url, headers=headers, verify=False)
        return res

Now, for methods methB and methC, I have mocked the rest call with the solution given here . But with methA() I am not able to patch the mock for two different REST call of two different type (GET and POST). What is the possible solution to mock this scenario? Thnaks in advance.

Use a mock API service that allows you to mock both POST and GET requests. Mocktastic allows you to do this. It is a downloadable desktop app for MacOS, Linux and Windows, which you can use to get a server running on your localhost, and easily mock the responses you wish to send.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM