Programming/Pytest

Requests API 테스트

c29130811 2024. 8. 15. 20:56

API를 테스트 하기 위해서는 http 관련 라이브러리가 필요한데, python 에서는 requests를 쓴다.

 

requests

Python HTTP for Humans.

pypi.org

node에 axios 같은?

import requests

def get_data_from_api(url):

    response = requests.get('https://api_url.com')
    response.raise_for_status() //https 오류가 발생하면 예외가 발생 됨 
    
    assert response["id"] == 1  # 응답에서 특정 키의 값 확인

 

raise_for_status() 는 bad request (a 4XX client error or 5XX server error response) 일 경우에는 

requests.exceptions.HTTPError: 404 Client Error

 

exception이 발생한다.

 

 

 

사실 python 보다는 역시나 postman이 짱짱..

 

다음엔 unittest.mock을 알아보즈아

728x90

'Programming > Pytest' 카테고리의 다른 글

pytest pytest.ini, conftest.py  (0) 2024.10.27
@pytest.fixture  (1) 2024.07.14
Assertion  (0) 2024.07.13
Pytest  (1) 2024.07.12