Programming/Pytest

pytest pytest.ini, conftest.py

c29130811 2024. 10. 27. 13:28

pytest 에서 설정 정의 등 여러가지 할 수 있게 도와주는 대표적(?) 파일들

 

1. pytest.ini 

pytest의 기본 설정을 정의 (음 node package.json이 생각났음..)

# pytest.ini
[pytest]
addopts = -v --tb=short --maxfail=1
testpaths = tests
markers =
    regression: 회귀 테스트용 마커
pythonpath = .
  • addopts: 기본 옵션을 전달함, 적힌 옵션이 pytest 터미널에 입력 시 적용되는 것들이다.
  • testpaths: pytest가 테스트를 자동으로 찾을 디렉토리를 지정 
  • markers: 사용자 정의 마커이며 @pytest.mark.regression로 적힌 test를 모아서 진행할 수 있고, pytest -m <marker> 명으로 실행하게 된다.
  • pythonpath: python 경로 설정하여 모듈을 쉽게 import하게 함.

2. conftest.py

테스트에서 공통적으로 사용할 수 있는 fixture, hook을 정의하고 사용

역시 나도 헷갈렸는데 fixture란 테스트를 하기 위해 필요한 설정 들을 의미 한다. 보통 데이터 베이스 연결, 테스트 데이터 생성, 설정 초기화 등을 의미 하게 된다.

 

사실 사용법은

 

https://19880811.tistory.com/entry/pytestfixture

 

@pytest.fixture

Pytest Fixture란?fixture는 setup과 teardown 작업을 수행하는 데 사용되는 데코레이터로 사용된다.즉, 아래와 같은 형태로 sampe_fixture에서 return한 data 를 test_sample의 인자로 전달 후, 사용할 수 있다는 의

19880811.tistory.com

 

잠깐 남겼었는데, 이번 playwright로 하면서 chatgpt 참고하면서 좀 뒤져본거같다.

 

@pytest.fixture(scope="session")
def google_login_develop(context):
    page = context.new_page()  # 새 페이지를 세션 동안 유지
    page.goto('https://블라블라블라.com/login')
    print("Navigated to development login page")

    with context.expect_page() as popup_info:
        time.sleep(3)
        page.click('button:has-text("Google로 계속하기")')

    popup = popup_info.value
    popup.fill('input[type="email"]', GOOGLE_EMAIL)
    popup.click('button:has-text("다음")')
    popup.fill('input[type="password"]', GOOGLE_PASSWORD)
    popup.click('button:has-text("다음")')
    popup.wait_for_load_state('networkidle')
    popup.click('button:has-text("계속")')
    print("Login completed on develop page")

    page.bring_to_front()
    popup.close()

    yield page

 

conftest.py에 구글 로그인 fixture로 정의한 것인데,

# tests/test_login.py
import pytest

@pytest.mark.run(order=1)
def test_create_inbound(google_login_develop):
    print('login 1번째')
    develop_page = google_login_develop
    develop_page.goto('https://블라블라.com/create')

    develop_page.wait_for_selector('input[name="customerName"]')

 

 

pytest를 실행하면 test_create_inbound가 실행될때 전달된 인자를 실행하는데, conftest.py를 실행해서 yield 시점부터 반환된것을 test_login에서 진행하게 된다.

 

pytest 좀 짱

 

728x90

'Programming > Pytest' 카테고리의 다른 글

Requests API 테스트  (0) 2024.08.15
@pytest.fixture  (1) 2024.07.14
Assertion  (0) 2024.07.13
Pytest  (1) 2024.07.12