Python에서 test코드를 작성하기 위하여 만들어진 framework이다.
pip install -U pytest
설치하여 사용할 수 있다.
함수 이름에 test_를 붙여서 사용한다.
test에 성공할 경우이다.
# test.py
import pytest
def func(x, y):
return x + y
def test_func():
assert func(2, 3) == 5
pytest test.py
=================================================================================== test session starts ====================================================================================
platform win32 -- Python 3.12.2, pytest-8.2.1, pluggy-1.5.0
rootdir: C:\workspace\pytest-study
collected 1 item
test.py . [100%]
==================================================================================== 1 passed in 0.01s =====================================================================================
test에 실패할 경우이다.
# test.py
import pytest
def func(x, y):
return x + y
def test_func():
assert func(2, 3) == 6 # 잘못된 값
=================================================================================== test session starts ====================================================================================
platform win32 -- Python 3.12.2, pytest-8.2.1, pluggy-1.5.0
rootdir: C:\workspace\pytest-study
collected 1 item
test.py F [100%]
========================================================================================= FAILURES =========================================================================================
________________________________________________________________________________________ test_func _________________________________________________________________________________________
def test_func():
> assert func(2, 3) == 6 # 잘못된 값
E assert 5 == 6
E + where 5 = func(2, 3)
test.py:7: AssertionError
================================================================================= short test summary info ==================================================================================
FAILED test.py::test_func - assert 5 == 6
==================================================================================== 1 failed in 0.04s =====================================================================================
class 이름에 Test를 붙여서 사용한다.
test에 성공할 경우이다.
import pytest
class TestClass:
def test_func(self):
assert True
def test_func2(self):
assert True
=================================================================================== test session starts ====================================================================================
platform win32 -- Python 3.12.2, pytest-8.2.1, pluggy-1.5.0
rootdir: C:\workspace\pytest-study
collected 2 items
test.py .. [100%]
==================================================================================== 2 passed in 0.01s =====================================================================================
test에 실패할 경우이다.
import pytest
class TestClass:
def test_func(self):
assert True
def test_func2(self):
assert False # 잘못된 값
=================================================================================== test session starts ====================================================================================
platform win32 -- Python 3.12.2, pytest-8.2.1, pluggy-1.5.0
rootdir: C:\workspace\pytest-study
collected 2 items
test.py .F [100%]
========================================================================================= FAILURES =========================================================================================
___________________________________________________________________________________ TestClass.test_func2 ___________________________________________________________________________________
self = <test.TestClass object at 0x0000022FE6F288F0>
def test_func2(self):
> assert False # 잘못된 값
E assert False
test.py:8: AssertionError
================================================================================= short test summary info ==================================================================================
FAILED test.py::TestClass::test_func2 - assert False
=============================================================================== 1 failed, 1 passed in 0.04s ================================================================================
pytest로 코드를 테스트 할 수 있다.
pytest에는 더 많은 기능들이 있으니 다음에 알아보도록 하자.
https://binux.tistory.com/47
https://velog.io/@jihwankim94/Pytest-%EC%95%8C%EC%95%84%EB%B3%B4%EA%B8%B0