T
T
tarp202022-02-08 14:45:58
Python
tarp20, 2022-02-08 14:45:58

What is the best practice for api testing?

there is a function

def is_car_exist(make, model):
    
    url = f'https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMake/\
    {make.capitalize()}?format=json'
    data = requests.get(url).json()['Results']
    return any(model.capitalize() == car['Model_Name'] for car in data)


what is the best way to test the request itself to an external api???

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2022-02-08
@Akela_wolf

This one - no way. At a minimum, you need to get rid of the hardcoded domain and put it in the config. Further options are possible:
1. Wiremock - creates a local server and responds to requests to it. That is, it "pretends to be" an external service for your program.
2. Unit tests. But for them, it is necessary to isolate calls to the external service and "close" them with an interface, instead of which the test implementation of the external service will be substituted.

V
Vladimir Kuts, 2022-02-08
@fox_12

Mockup requests to external resources

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question