[Python]pytest(1) 함수형 테스트와 클래스형 테스트

변도진·2024년 6월 3일
0

Python

목록 보기
3/13
post-thumbnail

pytest

Python에서 test코드를 작성하기 위하여 만들어진 framework이다.

pip install -U pytest

설치하여 사용할 수 있다.

function으로 사용

함수 이름에 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로 사용

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에는 더 많은 기능들이 있으니 다음에 알아보도록 하자.

Reference

https://binux.tistory.com/47
https://velog.io/@jihwankim94/Pytest-%EC%95%8C%EC%95%84%EB%B3%B4%EA%B8%B0

profile
낚시하고 싶다.

0개의 댓글