test를 진행하면 중복된 코드를 작성하는 경우가 생긴다.
fixture를 사용하여 중복된 부분을 쉽게 반복 할 수 있다.
수정 전
import pytest
class Calculator():
def add(self, x, y): return x + y
def sub(self, x, y): return x - y
def test_add():
cal = Calculator()
assert cal.add(1, 2) == 3
def test_sub():
cal = Calculator()
assert cal.sub(2, 1) == 1
=============================================================================== 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 ================================================================================
수정 후
import pytest
class Calculator():
def add(self, x, y): return x + y
def sub(self, x, y): return x - y
@pytest.fixture
def cal(): # fixture로 선언
return Calculator()
def test_add(cal): # 매개변수로 cal() 값을 받아옴
assert cal.add(1, 2) == 3
def test_sub(cal):
assert cal.sub(2, 1) == 1
=============================================================================== 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 ================================================================================
중복되었던 cal = Calculator()
부분을 fixture를 선언하여 해결하였다.
선언된 fixture를 conftest.py 파일에 한번에 모아 관리 할 수 있다.
# conftest.py
import pytest
class Calculator():
def add(self, x, y): return x + y
def sub(self, x, y): return x - y
@pytest.fixture
def cal(): # fixture로 선언
return Calculator()
# test.py
def test_add(cal):
assert cal.add(1, 2) == 3
def test_sub(cal):
assert cal.sub(2, 1) == 1
=============================================================================== 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.02s ================================================================================
conftest.py에 선언하여 test.py에서도 cal()을 사용할 수 있게 되었다.
yeild를 사용하면 test 진행 후 정리 작업을 수행 할 수 있다.
# conftest.py
import pytest
@pytest.fixture
def file(): # fixture로 선언
file = open("storage.txt", "r")
yield file
file.close()
# test.py
def test_read_file(file):
assert file.read() == "hello"
# storage.txt
hello
=============================================================================== 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.02s ================================================================================
fixture를 사용하여 중복된 코드를 제거하고 더욱 깔끔한 test 코드를 작성하자
https://velog.io/@jihwankim94/Pytest-%EC%95%8C%EC%95%84%EB%B3%B4%EA%B8%B0
https://sangjuncha-dev.github.io/posts/programming/python/2022-02-08-python-pytest-guide/