Pytest란?
Python 코드를 단위 테스트 (= 유닛테스트) 하기 위한 TEST Framework로,
Python 기반 프로젝트의 TDD (Test Driven Development)에 주로 사용
[SW 개발] 테스트 중심의 개발 TDD (with. Unit, E2E, Integration)
"의도하지 않은 결함 수가 많아지면 개발자는 변경을 주저한다" 테스트 주도 개발 (TDD) 란? Test Driven Development 의 약자로 테스트 주도 개발이란는 뜻을 가지는 소프트웨어 개발 방법론 중 하나이
yubi5050.tistory.com
Pytest 특징
- 테스트를 작성하는 데 있어 함수만 정의하면 되므로 편리 (단, pytest만의 고유한 방식을 익혀야 함)
- assert 문 재작성으로 인한 여러 input, output에 대해 테스트 가능
- 매개변수화된 fixture 제공 (fixture란 테스트 간, 공통적으로 이용되는 자원)
- 파일/함수에 test_로 시작되는 파일/함수를 테스트 코드로 인식하여 실행됨
- pytest 예약 함수 - setup_function(), teardown_function()로 각 테스트 함수 실행 전/후 실행 가능
- pytest code를 병렬로 실행 가능 (pytest-xdist 플러그인 추가 설치 필요)
Python Framework들의 Pytest 사용
일반적인 python 코드와 Flask에서는 pytest 설치 후 사용 가능
$ pip install pytest
Django에서의 pytest 사용시 추가 plugin 설치 필요
$ pip install pytest-django
https://pytest-django.readthedocs.io/en/latest/
pytest-django Documentation — pytest-django documentation
© Copyright 2022, Andreas Pelme and contributors. Revision 1f3708ab.
pytest-django.readthedocs.io
FastAPI에서는 자체 제공하는 TestClient나 Pytest를 이용해 진행 가능
https://fastapi.tiangolo.com/tutorial/testing/
Testing - FastAPI
Testing Thanks to Starlette, testing FastAPI applications is easy and enjoyable. It is based on Requests, so it's very familiar and intuitive. With it, you can use pytest directly with FastAPI. Using TestClient Info To use TestClient, first install request
fastapi.tiangolo.com
Pytest 관련 추가 파일
pytest.ini : pytest의 옵션을 명시하는 파일
conftest.py : 테스트 수행시 공통의 Fixture 등을 모아두는 파일
AssertionError 는 기본적으로 결과가 False, 0 None 등 일 때 가 발생
'Python > Advanced' 카테고리의 다른 글
[Python 비동기] (1) 코루틴, 비동기 관련 용어 이해 (0) | 2023.02.08 |
---|---|
[Pytest] 4. Pytest with Django (Feat. pytest-Django) (0) | 2022.11.06 |
[Pytest] 3. Pytest 문법 (Feat. fixture, parametize) (0) | 2022.11.06 |
[Pytest] 2. Pytest 명령어 옵션 (command) (0) | 2022.11.05 |
[Python 심화] Python의 GIL과 느린 이유 (0) | 2022.08.24 |