🎪 놀고있네/Python
[Python] Pytest 사용해보기(Conftest)
냥장판
2024. 5. 3. 11:53
반응형
Pytest는 fixture는 다른 폴더나 모듈에서 공통으로 참조할 수 있다는 점이 핵심인데.
따로 conftest.py 파일에서 fixture 를 사용할 수 있다.
예제를 작성해보면
아래와 같은 test_sample.py 파일이 있다
import pytest
@pytest.fixture
def common_data():
return [1, 2, 3, 4]
class Test_Class0:
def test_A(self, common_data):
assert sum(common_data) == 15
def test_B(self, common_data):
assert max(common_data) == 4
class Test_Case1:
def test_A(self, common_data):
assert 2 in common_data
이를 conftest.py 파일과 실제 테스트 케이스들만 모아져있는 test_sample.py 로 나눌 수 있다.
아래 그림에서 위에 초록색을 conftest.py 로
아래 노란색을 test_sample.py 로

pytest를 실행해보면 결과는 동일하다.
반응형