python/응용
pytest - fixture
wefree
2022. 5. 23. 17:20
기본 사용
import pytest
@pytest.fixture()
def setup():
print("\nSetup ===============")
def test1(setup):
print("Execute test1")
assert True
test_cal.py::test1
Setup ===============
PASSED [100%]Execute test1
yield 사용
import pytest
@pytest.fixture()
def setup2():
print("\nBefore ====================")
yield
print("\nAfter =====================")
def test2(setup2):
print("Execute test2")
assert True
test_cal.py::test2
Before ====================
PASSED [100%]Execute test2
After =====================
params 사용
import pytest
@pytest.fixture(params=[1, 2, 3])
def setup3(request):
ret = request.param
print(f"\nret={ret}")
return ret
def test3(setup3):
print(f"\nsetup3 = {setup3}")
assert True
test_cal.py::test3[1]
ret=1
PASSED [ 33%]
setup3 = 1
test_cal.py::test3[2]
ret=2
PASSED [ 66%]
setup3 = 2
test_cal.py::test3[3]
ret=3
PASSED [100%]
setup3 = 3
skip slow test
- pytest-skip-slow install
pip install pytest-skip-slow
- 코드 작성
import time
import pytest
def my_slow_func():
time.sleep(5)
return True
@pytest.mark.slow
def test_my_slow_func():
assert my_slow_func()
- 실행
- slow test 제외(default): pytest -v test_slow.py
- slow test 포함: pytest -v --slow test_slow.py
- 참고
- pytest-skip-slow package 를 이용하지 않고 pytest 에서 지원하는 기능을 이용해서도 slow test 를 skip 할 수 있다.