728x90

Programming/Pytest 5

pytest pytest.ini, conftest.py

pytest 에서 설정 정의 등 여러가지 할 수 있게 도와주는 대표적(?) 파일들 1. pytest.ini pytest의 기본 설정을 정의 (음 node package.json이 생각났음..)# pytest.ini[pytest]addopts = -v --tb=short --maxfail=1testpaths = testsmarkers = regression: 회귀 테스트용 마커pythonpath = .addopts: 기본 옵션을 전달함, 적힌 옵션이 pytest 터미널에 입력 시 적용되는 것들이다.testpaths: pytest가 테스트를 자동으로 찾을 디렉토리를 지정 markers: 사용자 정의 마커이며 @pytest.mark.regression로 적힌 test를 모아서 진행할 수 있고, pytes..

Programming/Pytest 2024.10.27

Requests API 테스트

API를 테스트 하기 위해서는 http 관련 라이브러리가 필요한데, python 에서는 requests를 쓴다. requestsPython HTTP for Humans.pypi.orgnode에 axios 같은?import requestsdef 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 resp..

Programming/Pytest 2024.08.15

@pytest.fixture

Pytest Fixture란?fixture는 setup과 teardown 작업을 수행하는 데 사용되는 데코레이터로 사용된다.즉, 아래와 같은 형태로 sampe_fixture에서 return한 data 를 test_sample의 인자로 전달 후, 사용할 수 있다는 의미import pytest@pytest.fixturedef sample_fixture(): data = {"key": "value"} return datadef test_sample(sample_fixture): assert sample_fixture["key"] == "value" 어떻게 보면 cypress 등에 beforeEach 등으로 보면 될 듯. 말그대로 테스트 시작전 setup을 하거나 하는 형태로, 물론 위에 써둔 ..

Programming/Pytest 2024.07.14

Assertion

https://docs.pytest.org/en/stable/how-to/assert.html pytest 를 사용하면 표준 Python assert에 따라 값을 검증 할 수 있다고 한다.# content of test_assert1.pydef f(): return 3def test_function(): assert f() == 4 기본적인 assert 문은 조건이 참인지 확인하는데, 조건이 거짓이면 실패 메시지를 생성하고 테스트를 실패로 처리함.또한 아래 처럼 , 를 통해 사용자 메시지를 지정할 수도 있음def test_custom_message(): assert 1 + 1 == 3, "Expected 3" # 실패 시 사용자 메세지 출력 리스트에 2가 있는지 확인def test_co..

Programming/Pytest 2024.07.13

Pytest

Python 에 있는 테스트 프레임워크 중 하나로 함수로 작성하면 되니까 생각보다 편리하다.unittest 는 Junit 을 참고한 테스트 프레임워크다보니, class 형태로 작성을 하며, 무려 python이지만 카멜케이스로 작성한다. 여어어어트으은.. 간략하게 pytest 맛보기 stable 사이트에 명시 된 require는 pytest requires: Python 3.8+ or PyPy3. 이렇다.https://docs.pytest.org/en/stable/index.html(얜 왜 미리보기 안됌...)테스트 환경에 python 프로젝트를 생성 후, 아래 pip를 통해 pytest를 설치한다.pip install -U pytest test_1.py 파일에 대충 공식 사이트에 있는 예제를 써보고 de..

Programming/Pytest 2024.07.12
728x90