PHpullh

PYTHON · 테스트

pytest 심화

pytest의 고급 기능: 마커, 플러그인, 설정, 실행 옵션을 다룹니다.

테스트중급pytest테스트markerassert

핵심 설명

pytest의 고급 기능: 마커, 플러그인, 설정, 실행 옵션을 다룹니다.

Python code

# pytest 테스트 예제 (실행: pytest test_example.py -v)
import pytest

# 기본 테스트
def test_addition():
    assert 1 + 1 == 2

def test_string():
    assert "hello".upper() == "HELLO"

# 예외 테스트
def test_zero_division():
    with pytest.raises(ZeroDivisionError):
        1 / 0

def test_value_error_message():
    with pytest.raises(ValueError, match="invalid"):
        raise ValueError("invalid input")

# 마커
@pytest.mark.slow
def test_slow_operation():
    import time
    time.sleep(0.01)
    assert True

@pytest.mark.skip(reason="아직 구현 안 됨")
def test_future_feature():
    pass

@pytest.mark.skipif(
    __import__("sys").version_info < (3, 11),
    reason="Python 3.11+ 필요"
)
def test_new_feature():
    assert True

# 근사값 비교
def test_floating_point():
    assert 0.1 + 0.2 == pytest.approx(0.3)
    assert [0.1, 0.2] == pytest.approx([0.1, 0.2], abs=1e-10)

# 실행: pytest -v --tb=short -k "not slow"
print("pytest 테스트 패턴 준비 완료")

학습 팁

pytest -k "keyword"로 특정 테스트만 실행하고, -x로 첫 실패 시 중단할 수 있습니다.

주의할 점

테스트 함수명이 test_로 시작하지 않으면 pytest가 자동 수집하지 않습니다.

자주 묻는 질문

pytest 심화란 무엇인가요?

pytest 의 고급 기능: 마커, 플러그인, 설정, 실행 옵션을 다룹니다.

pytest 심화 학습 시 주의할 점은 무엇인가요?

테스트 함수명이 test_ 로 시작하지 않으면 pytest가 자동 수집하지 않습니다.

Continue Learning

Python 학습을 이어가세요

총 200개의 독립 HTML 학습 문서 중 하나입니다. 각 문서는 고유 URL과 canonical 메타데이터를 가집니다.