Skip to content
Advertisement

Unittesting python?

I have some function with api

JavaScript

I have some integration tests:

JavaScript

Please could you show how Unittest will look like relatively mine function .

Advertisement

Answer

Solution

Use unittest.mock module. More details can be found here.

JavaScript

Explanation

patch replaces a real object or function with a mock one. It requires a positional argument target to be filled. This target needs to be importable by using path.to.module notation like normal import.

One way of using it is by using it as a decorator. When using as a decorator, you need to add a parameter to a decorated function. In this case, it is requests_get.

.return_value is used for function call and what you want it to return.

requests_get.return_value.json.return_value means that you want the return value of requests.get to be an object which has a method called json that returns {"Results": [{"Model_Name": "Civic"}]}

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement