다양한 프로그래밍 언어만큼 각 언어를 지원하는 테스트 프레임워크가 존재한다.
Pytest는 이름에서부터 느껴지듯이 Python 스크립트를 지원하는 테스트 프레임워크로,
쉽게 말해 작성한 Python 스크립트의 동작을 Pass / Fail 형식의 결과로 나타내주는 일종의 라이브러리 모듈이다.
APPIUM을 Python Client로 사용하고 있다 보니, 자연스럽게 Pytest를 접하게 되었는데
생각보다 활용범위가 넓어 잊어버리기 전에 조금 정리해보고자 한다.
Pytest 공식 문서
앞서 소개했듯이, Pytest는 Python 스크립트를 실행하면서 Pass / Fail 결과로 나타내주는 Python 테스트 프레임워크다.
사용 방법은 어렵지 않아 공식 문서를 보면 금방 이해 할 수 있다.
Full pytest documentation — pytest documentation
docs.pytest.org
Pytest 설치 방법
사전준비. 당연히 Python이 설치되어 있어야 한다.
※ Pytest를 지원하는 Python 버전은 Python 3.6, 3.7, 3.8, 3.9, PyPy3 이다.
1. Windows 명령 프롬프트 혹은 MacOS 터미널에 다음 명령어를 입력한다.
$ pip install -U pytest
2. 설치 과정이 끝나면, 다음 명령어를 입력하여 Pytest의 버전이 뜨는지(= 제대로 설치 되었는지) 확인한다.
$ pytest --version
pytest 6.1.2 // 제대로 설치 된 경우 출력
Pytest 사용 방법
일반 라이브러리를 사용하듯이 Python 스크립트에 pytest 모듈을 import하여 사용한다.
단, Python 스크립트 파일명과 테스트 결과를 확인할 함수명은 test_ 로 시작 혹은 _test로 끝나야 한다.
※ 파일명/함수명을 지키지 않으면, pytest에서 test 대상으로 인지하지 못해 다음과 같이 Collected 0 item 으로 나타난다.
$ pytest test.py
================================== test session starts =======================================
platform win32 -- Python 3.8.3, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: C:\Users\User
collected 0 items
================================= no tests ran in 0.02s ======================================
예시)
동일한 Python 스크립트를 일반 Python으로 실행 VS Pytest로 실행
# 파일명 : test.py
import pytest
def test_success():
test = 'Test Success !!'
print(test)
assert 'Success' in test // Test 결과 판별 기준
def test_fail():
test = 'Test Failed !!'
print(test)
assert 'Success' in test // Test 결과 판별 기준
test_success() 와 test_fail() 이라는 함수 두개만 선언되어 있고, main 에서 함수를 호출하지 않기 때문에
일반 Python으로 실행시켰을 때는 아무런 동작도 표시되지 않는다.
$ python test.py
test.py를 pytest로 실행하면, 다음과 같은 테스트 결과가 나타난다.
$ pytest test.py
===================================== test session starts =================================================
platform win32 -- Python 3.8.3, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: C:\Users\User
collected 2 items
PycharmProjects\test\test.py .F [100%]
========================================== FAILURES =======================================================
__________________________________________ test_fail ______________________________________________________
def test_fail():
test = 'Test Failed !!'
print(test)
> assert 'Success' in test
E AssertionError: assert 'Success' in 'Test Failed !!'
PycharmProjects\test\test.py:11: AssertionError
------------------------------------ Captured stdout call -------------------------------------------------
Test Failed !!
=================================== short test summary info ===============================================
FAILED PycharmProjects/test/test.py::test_fail - AssertionError: assert 'Success' in 'Test Failed !!'
================================= 1 failed, 1 passed in 0.13s =============================================
test_success() 함수의 결과는 Pass로 따로 출력되지 않았고,
test_fail() 함수의 결과만 어느 부분에서 테스트가 실패했는지 친절하게 테스트 결과가 표시 된다.
+)
테스트 결과뿐만 아니라, 콘솔에 내용을 출력하고 싶다면 -s 옵션을 사용한다.
$ pytest test.py -s
====================================== test session starts =================================================
platform win32 -- Python 3.8.3, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: C:\Users\HyeJungSeo
collected 2 items
PycharmProjects\test\test.py Test Success !! // test_success() 출력결과 확인
.Test Failed !! // test_fail() 출력결과 확인
F
========================================== FAILURES =======================================================
___________________________________________ test_fail ______________________________________________________
def test_fail():
test = 'Test Failed !!'
print(test)
> assert 'Success' in test
E AssertionError: assert 'Success' in 'Test Failed !!'
PycharmProjects\test\test.py:11: AssertionError
==================================== short test summary info ===============================================
FAILED PycharmProjects/test/test.py::test_fail - AssertionError: assert 'Success' in 'Test Failed !!'
================================== 1 failed, 1 passed in 0.08s =============================================
'곰대생 > Python' 카테고리의 다른 글
Python 디렉토리 생성 / 파일 쓰기 (0) | 2020.12.08 |
---|