pytest-xdist를 사용하여 동시에 테스트 실행
pytest-xdist: 병렬로 테스트 실행
[https://pypi.python.org/pypi/pytest-xdist]
자동화된 테스트에서 일부 리소스는 동시에 하나의 테스트 케이스에서만 액세스할 수 있으며, 동일한 리소스를 동시에 사용할 필요가 없는 경우 테스트 케이스를 병렬로 실행할 수 있습니다.
pip install pytest-xdist 명령을 실행하여 플러그인을 설치합니다.
E:\Programs\Python\Python_Pytest\TestScripts>pip install pytest-xdist
Collecting pytest-xdist
Downloading https://files.pythonhosted.org/packages/9f/cc/371b2e6dfbf4e8df07b04e310dd6ea0b3f367e257d1e6cb516b25bc4af1b/pytest_xdist-1.29.0-py2.py3-none-any.whl
Collecting pytest-forked (from pytest-xdist)
Downloading https://files.pythonhosted.org/packages/3f/55/ef92c340e723495dbee91d749903d2b7950b49c501943296257246d7d880/pytest_forked-1.0.2-py2.py3-none-any.whl
Requirement already satisfied: six in c:\python37\lib\site-packages (from pytest-xdist) (1.12.0)
Requirement already satisfied: pytest>=4.4.0 in c:\python37\lib\site-packages (from pytest-xdist) (4.5.0)
Collecting execnet>=1.1 (from pytest-xdist)
Downloading https://files.pythonhosted.org/packages/77/1a/f69e1f73bc36f55d3273afd1c52936def71ac67d9c5215be3a4ca3a45577/execnet-1.6.0-py2.py3-none-any.whl
Requirement already satisfied: setuptools in c:\users\davieyang\appdata\roaming\python\python37\site-packages (from pytest>=4.4.0->pytest-xdist) (41.0.1)
Requirement already satisfied: py>=1.5.0 in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (1.8.0)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (0.4.1)
Requirement already satisfied: attrs>=17.4.0 in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (19.1.0)
Requirement already satisfied: pluggy!=0.10,<1.0,>=0.9 in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (0.11.0)
Requirement already satisfied: atomicwrites>=1.0 in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (1.3.0)
Requirement already satisfied: wcwidth in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (0.1.7)
Requirement already satisfied: more-itertools>=4.0.0; python_version > "2.7" in c:\python37\lib\site-packages (from pytest>=4.4.0->pytest-xdist) (7.0.0)
Collecting apipkg>=1.4 (from execnet>=1.1->pytest-xdist)
Downloading https://files.pythonhosted.org/packages/67/08/4815a09603fc800209431bec5b8bd2acf2f95abdfb558a44a42507fb94da/apipkg-1.5-py2.py3-none-any.whl
Installing collected packages: pytest-forked, apipkg, execnet, pytest-xdist
Successfully installed apipkg-1.5 execnet-1.6.0 pytest-forked-1.0.2 pytest-xdist-1.29.0
pytest-xdist를 사용하여 테스트 실행
E:\Programs\Python\Python_Pytest\TestScripts>pytest -n auto
================================ test session starts ================================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, repeat-0.8.0, forked-1.0.2, allure-pytest-2.6.3
gw0 [17] / gw1 [17] / gw2 [17] / gw3 [17]
...........FF..F. [100%]
=========================== FAILURES ================================================
________________________________________ test_true ____________________________________________
[gw2] win32 -- Python 3.7.3 c:\python37\python.exe
def test_true():
> assert not is_prime(7)
E assert not True
E + where True = is_prime(7)
test_asserts.py:65: AssertionError
__________________________________________ test_add4 ___________________________________________
[gw3] win32 -- Python 3.7.3 c:\python37\python.exe
def test_add4():
> assert add(17,22) >= 50
E assert 39 >= 50
E + where 39 = add(17, 22)
test_asserts.py:34: AssertionError
______________________________ test_not_equal _____________________________________________
[gw1] win32 -- Python 3.7.3 c:\python37\python.exe
def test_not_equal():
> assert (1, 2, 3) == (3, 2, 1)
E assert (1, 2, 3) == (3, 2, 1)
E At index 0 diff: 1 != 3
E Use -v to get the full diff
test_one.py:9: AssertionError
============================== 3 failed, 14 passed, 4 warnings in 2.20 seconds ===============================
매개변수 설명
-n auto는 시스템의 CPU 수를 자동으로 감지합니다.
-n numprocesses는 테스트를 실행할 프로세서 프로세스 수를 지정합니다.
pytest-repeat를 사용하여 테스트 사례를 반복적으로 실행
pytest-repeat: 두 번 이상 테스트 실행
pip install pytest-repeat[https://pypi.python.org/pypi/pytest-repeat] 명령을 실행합니다.
E:\Programs\Python\Python_Pytest\TestScripts>pip install pytest-repeat
Collecting pytest-repeat
Downloading https://files.pythonhosted.org/packages/2e/de/c1d69002db74a99b3df0463e95066c03d82d9d2a53be738c140207134e0f/pytest_repeat-0.8.0-py2.py3-none-any.whl
Requirement already satisfied: pytest>=3.6 in c:\python37\lib\site-packages (from pytest-repeat) (4.5.0)
Requirement already satisfied: attrs>=17.4.0 in c:\python37\lib\site-packages (from pytest>=3.6->pytest-repeat) (19.1.0)
Requirement already satisfied: pluggy!=0.10,<1.0,>=0.9 in c:\python37\lib\site-packages (from pytest>=3.6->pytest-repeat) (0.11.0)
Requirement already satisfied: wcwidth in c:\python37\lib\site-packages (from pytest>=3.6->pytest-repeat) (0.1.7)
Requirement already satisfied: more-itertools>=4.0.0; python_version > "2.7" in c:\python37\lib\site-packages (from pytest>=3.6->pytest-repeat) (7.0.0)
Requirement already satisfied: py>=1.5.0 in c:\python37\lib\site-packages (from pytest>=3.6->pytest-repeat) (1.8.0)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\python37\lib\site-packages (from pytest>=3.6->pytest-repeat) (0.4.1)
Requirement already satisfied: setuptools in c:\users\davieyang\appdata\roaming\python\python37\site-packages (from pytest>=3.6->pytest-repeat) (41.0.1)
Requirement already satisfied: six>=1.10.0 in c:\python37\lib\site-packages (from pytest>=3.6->pytest-repeat) (1.12.0)
Requirement already satisfied: atomicwrites>=1.0 in c:\python37\lib\site-packages (from pytest>=3.6->pytest-repeat) (1.3.0)
Installing collected packages: pytest-repeat
Successfully installed pytest-repeat-0.8.0
테스트 실행, 명령 실행pytest --count=2 -v
E:\Programs\Python\Python_Pytest\TestScripts>pytest --count=2 -v
======================== test session starts==============================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0 -- c:\python37\python.exe
cachedir: .pytest_cache
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: repeat-0.8.0, allure-pytest-2.6.3
collected 34 items
test_asserts.py::test_add[1-2] PASSED [ 2%]
test_asserts.py::test_add[2-2] PASSED [ 5%]
test_asserts.py::test_add2[1-2] PASSED [ 8%]
test_asserts.py::test_add2[2-2] PASSED [ 11%]
test_asserts.py::test_add3[1-2] PASSED [ 14%]
test_asserts.py::test_add3[2-2] PASSED [ 17%]
test_asserts.py::test_add4[1-2] FAILED [ 20%]
test_asserts.py::test_add4[2-2] FAILED [ 23%]
test_asserts.py::test_in[1-2] PASSED [ 26%]
test_asserts.py::test_in[2-2] PASSED [ 29%]
test_asserts.py::test_not_in[1-2] PASSED [ 32%]
test_asserts.py::test_not_in[2-2] PASSED [ 35%]
test_asserts.py::test_true[1-2] FAILED [ 38%]
test_asserts.py::test_true[2-2] FAILED [ 41%]
test_fixture1.py::test_numbers_3_4[1-2] PASSED [ 44%]
test_fixture1.py::test_numbers_3_4[2-2] PASSED [ 47%]
test_fixture1.py::test_strings_a_3[1-2] PASSED [ 50%]
test_fixture1.py::test_strings_a_3[2-2] PASSED [ 52%]
test_fixture2.py::TestUM::test_numbers_5_6[1-2] PASSED [ 55%]
test_fixture2.py::TestUM::test_numbers_5_6[2-2] PASSED [ 58%]
test_fixture2.py::TestUM::test_strings_b_2[1-2] PASSED [ 61%]
test_fixture2.py::TestUM::test_strings_b_2[2-2] PASSED [ 64%]
test_one.py::test_equal[1-2] PASSED [ 67%]
test_one.py::test_equal[2-2] PASSED [ 70%]
test_one.py::test_not_equal[1-2] FAILED [ 73%]
test_one.py::test_not_equal[2-2] FAILED [ 76%]
test_two.py::test_default[1-2] PASSED [ 79%]
test_two.py::test_default[2-2] PASSED [ 82%]
test_two.py::test_member_access[1-2] PASSED [ 85%]
test_two.py::test_member_access[2-2] PASSED [ 88%]
test_two.py::test_asdict[1-2] PASSED [ 91%]
test_two.py::test_asdict[2-2] PASSED [ 94%]
test_two.py::test_replace[1-2] PASSED [ 97%]
test_two.py::test_replace[2-2] PASSED [100%]
============================================ FAILURES ====================================
________________________________ test_add4[1-2] _______________________________________
def test_add4():
> assert add(17,22) >= 50
E assert 39 >= 50
E + where 39 = add(17, 22)
test_asserts.py:34: AssertionError
_________________________ test_add4[2-2] ______________________________________________
def test_add4():
> assert add(17,22) >= 50
E assert 39 >= 50
E + where 39 = add(17, 22)
test_asserts.py:34: AssertionError
________________________ test_true[1-2] __________________________________________
def test_true():
> assert not is_prime(7)
E assert not True
E + where True = is_prime(7)
test_asserts.py:65: AssertionError
____________________________ test_true[2-2] ______________________________________________
def test_true():
> assert not is_prime(7)
E assert not True
E + where True = is_prime(7)
test_asserts.py:65: AssertionError
__________________________ test_not_equal[1-2] ____________________________________________
def test_not_equal():
> assert (1, 2, 3) == (3, 2, 1)
E assert (1, 2, 3) == (3, 2, 1)
E At index 0 diff: 1 != 3
E Full diff:
E - (1, 2, 3)
E ? ^ ^
E + (3, 2, 1)
E ? ^ ^
test_one.py:9: AssertionError
_________________________________ test_not_equal[2-2] ____________________________________
def test_not_equal():
> assert (1, 2, 3) == (3, 2, 1)
E assert (1, 2, 3) == (3, 2, 1)
E At index 0 diff: 1 != 3
E Full diff:
E - (1, 2, 3)
E ? ^ ^
E + (3, 2, 1)
E ? ^ ^
test_one.py:9: AssertionError
=================== 6 failed, 28 passed, 1 warnings in 0.77 seconds =======================
이는 테스트의 하위 집합을 반복하는 것과 비교할 수 있으며 후자의 특정 테스트는 밤에도 N 번 반복할 수 있으며 오류가 발생하면 중지될 수도 있습니다.
pytest-instafail을 사용하여 자세한 스택 정보를 확인하세요.
테스트 실행에 실패나 오류가 발생하면 적시에 자세한 스택 정보를 확인할 수 있습니다.
pytest-instafail:실패 및 오류 세부 정보 보기
[https://pypi.python.org/pypi/pytest-instafail
] pip install pytest-instafail 명령을 실행하여 플러그인을 설치합니다.
E:\Programs\Python\Python_Pytest\TestScripts>pip install pytest-instafail
Collecting pytest-instafail
Downloading https://files.pythonhosted.org/packages/fa/16/473621ad68cc2a1cb2888478e66db5080a06adf695470c8dd4ec669c25d5/pytest-instafail-0.4.1.tar.gz
Requirement already satisfied: pytest>=2.9 in c:\python37\lib\site-packages (from pytest-instafail) (4.5.0)
Requirement already satisfied: attrs>=17.4.0 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-instafail) (19.1.0)
Requirement already satisfied: more-itertools>=4.0.0; python_version > "2.7" in c:\python37\lib\site-packages (from pytest>=2.9->pytest-instafail) (7.0.0)
Requirement already satisfied: six>=1.10.0 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-instafail) (1.12.0)
Requirement already satisfied: atomicwrites>=1.0 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-instafail) (1.3.0)
Requirement already satisfied: setuptools in c:\users\davieyang\appdata\roaming\python\python37\site-packages (from pytest>=2.9->pytest-instafail) (41.0.1)
Requirement already satisfied: pluggy!=0.10,<1.0,>=0.9 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-instafail) (0.11.0)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\python37\lib\site-packages (from pytest>=2.9->pytest-instafail) (0.4.1)
Requirement already satisfied: wcwidth in c:\python37\lib\site-packages (from pytest>=2.9->pytest-instafail) (0.1.7)
Requirement already satisfied: py>=1.5.0 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-instafail) (1.8.0)
Building wheels for collected packages: pytest-instafail
Building wheel for pytest-instafail (setup.py) ... done
Stored in directory: C:\Users\davieyang\AppData\Local\pip\Cache\wheels\16\cb\de\3a1d2f5c992fedf9a86b8eead949a606a6c953228ac1fe0655
Successfully built pytest-instafail
Installing collected packages: pytest-instafail
Successfully installed pytest-instafail-0.4.1
일반적으로 pytest를 실행한 후 오류 및 실패한 테스트 케이스의 스택 정보가 표시되는데, 테스트 케이스가 많고 실행 시간이 너무 길면 끝까지 스택 추적 정보를 볼 수 없기를 바랄 가능성이 높습니다. 이 플러그인을 사용하여 테스트할 수 있습니다. 실행이 실패하면 예외 정보가 즉시 표시됩니다.
테스트 실행
E:\Programs\Python\Python_Pytest\TestScripts>pytest --tb=line --instafail
========================= test session starts ========================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, instafail-0.4.1, forked-1.0.2, allure-pytest-2.6.3
collected 17 items
test_asserts.py ...F
E:\Programs\Python\Python_Pytest\TestScripts\test_asserts.py:34: assert 39 >= 50
test_asserts.py ..F
E:\Programs\Python\Python_Pytest\TestScripts\test_asserts.py:65: assert not True
test_fixture1.py .. [ 52%]
test_fixture2.py .. [ 64%]
test_one.py .F
E:\Programs\Python\Python_Pytest\TestScripts\test_one.py:9: assert (1, 2, 3) == (3, 2, 1)
test_two.py .... [100%]
=================== 3 failed, 14 passed, 1 warnings in 0.32 seconds =======================
pytest-timeout을 사용하여 테스트 실행 시간 제한 설정
pytest-timeout: 테스트에 시간 제한을 두세요
[https://pypi.python.org/pypi/pytest-time]
명령을 실행 pip install pytest-timeout
하고 플러그인을 설치합니다.
E:\Programs\Python\Python_Pytest\TestScripts>pip install pytest-timeout
Collecting pytest-timeout
Downloading https://files.pythonhosted.org/packages/58/92/f60ea2e27074d6f97c8aaf21e34d1f838eb623e4b8070680846c65318a10/pytest_timeout-1.3.3-py2.py3-none-any.whl
Requirement already satisfied: pytest>=3.6.0 in c:\python37\lib\site-packages (from pytest-timeout) (4.5.0)
Requirement already satisfied: attrs>=17.4.0 in c:\python37\lib\site-packages (from pytest>=3.6.0->pytest-timeout) (19.1.0)
Requirement already satisfied: atomicwrites>=1.0 in c:\python37\lib\site-packages (from pytest>=3.6.0->pytest-timeout) (1.3.0)
Requirement already satisfied: six>=1.10.0 in c:\python37\lib\site-packages (from pytest>=3.6.0->pytest-timeout) (1.12.0)
Requirement already satisfied: py>=1.5.0 in c:\python37\lib\site-packages (from pytest>=3.6.0->pytest-timeout) (1.8.0)
Requirement already satisfied: more-itertools>=4.0.0; python_version > "2.7" in c:\python37\lib\site-packages (from pytest>=3.6.0->pytest-timeout) (7.0.0)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\python37\lib\site-packages (from pytest>=3.6.0->pytest-timeout) (0.4.1)
Requirement already satisfied: wcwidth in c:\python37\lib\site-packages (from pytest>=3.6.0->pytest-timeout) (0.1.7)
Requirement already satisfied: pluggy!=0.10,<1.0,>=0.9 in c:\python37\lib\site-packages (from pytest>=3.6.0->pytest-timeout) (0.11.0)
Requirement already satisfied: setuptools in c:\users\davieyang\appdata\roaming\python\python37\site-packages (from pytest>=3.6.0->pytest-timeout) (41.0.1)
Installing collected packages: pytest-timeout
Successfully installed pytest-timeout-1.3.3
주문 실행pytest --timeout=0.1
E:\Programs\Python\Python_Pytest\TestScripts>pytest --timeout=0.1
======================== test session starts =============================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, forked-1.0.2, allure-pytest-2.6.3
timeout: 0.1s
timeout method: thread
timeout func_only: False
collected 17 items
test_asserts.py ...F..F [ 41%]
test_fixture1.py .. [ 52%]
test_fixture2.py .. [ 64%]
test_one.py .F [ 76%]
test_two.py .... [100%]
================================== FAILURES ===============================================
_________________________________ test_add4 ______________________________________________
def test_add4():
> assert add(17,22) >= 50
E assert 39 >= 50
E + where 39 = add(17, 22)
test_asserts.py:34: AssertionError
______________________________ test_true ______________________________________________
def test_true():
> assert not is_prime(7)
E assert not True
E + where True = is_prime(7)
test_asserts.py:65: AssertionError
________________________________ test_not_equal _________________________________________
def test_not_equal():
> assert (1, 2, 3) == (3, 2, 1)
E assert (1, 2, 3) == (3, 2, 1)
E At index 0 diff: 1 != 3
E Use -v to get the full diff
test_one.py:9: AssertionError
==================== 3 failed, 14 passed, 1 warnings in 0.32 seconds ======================
Tox를 사용하여 여러 환경에서 사용 사례 실행
Tox는 여러 구성을 테스트합니다.
tox는 다양한 Python 버전뿐만 아니라 여러 환경에서 테스트를 실행할 수 있는 명령줄 도구입니다. 이를 사용하여 다양한 종속성 구성과 다양한 운영 체제 구성을 테스트할 수 있습니다. 작동 방식은 setup.py 파일을 통해 이루어집니다. 테스트 중인 프로그램의 소스 코드 설치 패키지 tox.ini의 모든 환경 설정을 확인하고 각 환경 설정에 대해 다음 작업을 수행합니다.
톡스 설치
C:\Users\davieyang>pip install tox
Collecting tox
Using cached https://files.pythonhosted.org/packages/a7/0c/ed234b83d2c4fcef1cfccf97371183d51dafae62e64334de34d0a6333114/tox-3.14.0-py2.py3-none-any.whl
Collecting importlib-metadata<1,>=0.12; python_version < "3.8" (from tox)
Downloading https://files.pythonhosted.org/packages/f6/d2/40b3fa882147719744e6aa50ac39cf7a22a913cbcba86a0371176c425a3b/importlib_metadata-0.23-py2.py3-none-any.whl
Requirement already satisfied: py<2,>=1.4.17 in c:\python37\lib\site-packages (from tox) (1.8.0)
Requirement already satisfied: six<2,>=1.0.0 in c:\python37\lib\site-packages (from tox) (1.12.0)
Collecting toml>=0.9.4 (from tox)
Using cached https://files.pythonhosted.org/packages/a2/12/ced7105d2de62fa7c8fb5fce92cc4ce66b57c95fb875e9318dba7f8c5db0/toml-0.10.0-py2.py3-none-any.whl
Requirement already satisfied: packaging>=14 in c:\python37\lib\site-packages (from tox) (19.0)
Collecting filelock<4,>=3.0.0 (from tox)
Downloading https://files.pythonhosted.org/packages/93/83/71a2ee6158bb9f39a90c0dea1637f81d5eef866e188e1971a1b1ab01a35a/filelock-3.0.12-py3-none-any.whl
Collecting virtualenv>=14.0.0 (from tox)
Downloading https://files.pythonhosted.org/packages/8b/12/8d4f45b8962b03ac9efefe5ed5053f6b29334d83e438b4fe379d21c0cb8e/virtualenv-16.7.5-py2.py3-none-any.whl (3.3MB)
100% |████████████████████████████████| 3.3MB 9.9kB/s
Collecting pluggy<1,>=0.12.0 (from tox)
Downloading https://files.pythonhosted.org/packages/92/c7/48439f7d5fd6bddb4c04b850bb862b42e3e2b98570040dfaf68aedd8114b/pluggy-0.13.0-py2.py3-none-any.whl
Collecting zipp>=0.5 (from importlib-metadata<1,>=0.12; python_version < "3.8"->tox)
Downloading https://files.pythonhosted.org/packages/74/3d/1ee25a26411ba0401b43c6376d2316a71addcc72ef8690b101b4ea56d76a/zipp-0.6.0-py2.py3-none-any.whl
Requirement already satisfied: pyparsing>=2.0.2 in c:\python37\lib\site-packages (from packaging>=14->tox) (2.4.0)
Requirement already satisfied: more-itertools in c:\python37\lib\site-packages (from zipp>=0.5->importlib-metadata<1,>=0.12; python_version < "3.8"->tox) (7.0.0)
Installing collected packages: zipp, importlib-metadata, toml, filelock, virtualenv, pluggy, tox
Found existing installation: pluggy 0.11.0
Uninstalling pluggy-0.11.0:
Successfully uninstalled pluggy-0.11.0
Successfully installed filelock-3.0.12 importlib-metadata-0.23 pluggy-0.13.0 toml-0.10.0 tox-3.14.0 virtualenv-16.7.5 zipp-0.6.0
tox.ini 구성
- tox 디렉터리에 가상 환경 생성
- pip를 사용하여 종속 패키지 설치
- pip를 사용하여 1단계의 가상 환경에 자신만의 패키지를 설치하세요.
- 테스트 케이스 실행
- 모든 환경을 테스트한 후 tox는 테스트 결과에 대한 요약을 생성하고,
프로젝트 파일 상단에 tox.ini 파일을 추가(setup.py와 동일한 디렉터리에 배치)한 후 tox에 다음 내용을 작성합니다. .ini 파일
;---
; Excerpted from "Python Testing with pytest",
; published by The Pragmatic Bookshelf.
; Copyrights apply to this code. It may not be used to create training material,
; courses, books, articles, and the like. Contact us if you are in doubt.
; We make no guarantees that this code is fit for any purpose.
; Visit http://www.pragmaticprogrammer.com/titles/bopytest for more book information.
;---
# tox.ini , put in same dir as setup.py
[tox]
envlist = py27,py37
[testenv]
deps=pytest
commands=pytest
[pytest]
addopts = -rsxX -l --tb=short --strict
markers =
smoke: Run the smoke test test functions
get: Run the test functions that test tasks.get()
옵션 | 설명하다 |
---|---|
envlist=py27,py36 |
python2.7 및 python3.6을 사용하여 테스트 사례를 실행함을 나타냅니다. |
deps=pytest |
tox가 pytest가 설치되었는지 확인하도록 한다는 의미이며, 종속성이 여러 개인 경우 계속 동일한 방식으로 나열할 수 있으며 버전도 지정할 수 있습니다. |
commands=pytest |
각 환경에서 pytest를 실행하도록 tox에게 지시합니다. |
톡스를 실행하다
그런 다음 tox.ini가 있는 경로에서 tox 명령을 실행하면 지정된 두 가지 환경을 사용하여 경로 아래의 테스트 폴더에 있는 모든 테스트 케이스가 실행됩니다.
(venv) E:\Programs\Python\Python_Pytest\SourceCode\ch7\tasks_proj_v2>tox
GLOB sdist-make: E:\Programs\Python\Python_Pytest\SourceCode\ch7\tasks_proj_v2\setup.py
py37 create: E:\Programs\Python\Python_Pytest\SourceCode\ch7\tasks_proj_v2\.tox\py37
py37 installdeps: pytest
......
----------------------------------Summary----------------------------------------------------------------------------------
py27: commands succeeded
py36: commands succeeded
congratulations :)
pytest-sugar를 사용하여 테스트 결과를 더욱 흥미롭게 만드세요
pytest-sugar: instafail+색상+진행률 표시줄
플러그인을 설치하려면 pip install pytest-sugar 명령을 사용하십시오.
E:\Programs\Python\Python_Pytest\TestScripts>pip install pytest-sugar
Collecting pytest-sugar
Downloading https://files.pythonhosted.org/packages/da/3b/f1e3c8830860c1df8f0e0f6713932475141210cfa021e362ca2774d2bf02/pytest_sugar-0.9.2-py2.py3-none-any.whl
Requirement already satisfied: pytest>=2.9 in c:\python37\lib\site-packages (from pytest-sugar) (4.5.0)
Collecting packaging>=14.1 (from pytest-sugar)
Downloading https://files.pythonhosted.org/packages/91/32/58bc30e646e55eab8b21abf89e353f59c0cc02c417e42929f4a9546e1b1d/packaging-19.0-py2.py3-none-any.whl
Collecting termcolor>=1.1.0 (from pytest-sugar)
Downloading https://files.pythonhosted.org/packages/8a/48/a76be51647d0eb9f10e2a4511bf3ffb8cc1e6b14e9e4fab46173aa79f981/termcolor-1.1.0.tar.gz
Requirement already satisfied: atomicwrites>=1.0 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-sugar) (1.3.0)
Requirement already satisfied: wcwidth in c:\python37\lib\site-packages (from pytest>=2.9->pytest-sugar) (0.1.7)
Requirement already satisfied: attrs>=17.4.0 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-sugar) (19.1.0)
Requirement already satisfied: six>=1.10.0 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-sugar) (1.12.0)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\python37\lib\site-packages (from pytest>=2.9->pytest-sugar) (0.4.1)
Requirement already satisfied: py>=1.5.0 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-sugar) (1.8.0)
Requirement already satisfied: setuptools in c:\users\davieyang\appdata\roaming\python\python37\site-packages (from pytest>=2.9->pytest-sugar) (41.0.1)
Requirement already satisfied: pluggy!=0.10,<1.0,>=0.9 in c:\python37\lib\site-packages (from pytest>=2.9->pytest-sugar) (0.11.0)
Requirement already satisfied: more-itertools>=4.0.0; python_version > "2.7" in c:\python37\lib\site-packages (from pytest>=2.9->pytest-sugar) (7.0.0)
Collecting pyparsing>=2.0.2 (from packaging>=14.1->pytest-sugar)
Downloading https://files.pythonhosted.org/packages/dd/d9/3ec19e966301a6e25769976999bd7bbe552016f0d32b577dc9d63d2e0c49/pyparsing-2.4.0-py2.py3-none-any.whl (62kB)
100% |████████████████████████████████| 71kB 19kB/s
Building wheels for collected packages: termcolor
Building wheel for termcolor (setup.py) ... done
Stored in directory: C:\Users\davieyang\AppData\Local\pip\Cache\wheels\7c\06\54\bc84598ba1daf8f970247f550b175aaaee85f68b4b0c5ab2c6
Successfully built termcolor
Installing collected packages: pyparsing, packaging, termcolor, pytest-sugar
Successfully installed packaging-19.0 pyparsing-2.4.0 pytest-sugar-0.9.2 termcolor-1.1.0
테스트 실행
E:\Programs\Python\Python_Pytest\TestScripts>pytest
Test session starts (platform: win32, Python 3.7.3, pytest 4.5.0, pytest-sugar 0.9.2)
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, sugar-0.9.2, repeat-0.8.0, instafail-0.4.1, forked-1.0.2, allure-pytest-2.6.3
collecting ...
test_asserts.py ✓ 6% ▋
test_asserts.py ✓✓ 12% █▎
test_asserts.py ✓✓✓ 18% █▊
――――――――――――――――――――――――――――――――――――――――――test_add4――――――――――――――――――――――――――――――――――――――――
def test_add4():
> assert add(17,22) >= 50
E assert 39 >= 50
E + where 39 = add(17, 22)
test_asserts.py:34: AssertionError
test_asserts.py ⨯ 24% ██▍
test_asserts.py ⨯✓ 29% ██▉
test_asserts.py ⨯✓✓ 35% ███▌
――――――――――――――――――――――――――――――――――――――― test_true――――――――――――――――――――――――――――――――――――――――――
def test_true():
> assert not is_prime(7)
E assert not True
E + where True = is_prime(7)
test_asserts.py:65: AssertionError
test_asserts.py ⨯ 41% ████▎
test_fixture1.py ✓ 47% ████
test_fixture1.py ✓✓ 53% ████
█▍
test_fixture2.py ✓ 59% ████
test_fixture2.py ✓✓ 65% ████
██▌
test_one.py ✓ 71% ████
███▏
―――――――――――――――――――――――――――――――――――― test_not_equal ―――――――――――――――――――――――――――――――――――――――
def test_not_equal():
> assert (1, 2, 3) == (3, 2, 1)
E assert (1, 2, 3) == (3, 2, 1)
E At index 0 diff: 1 != 3
E Use -v to get the full diff
test_one.py:9: AssertionError
test_one.py ⨯ 76% █████
██▋
test_two.py ✓ 82% ████
test_two.py ✓✓ 88% ████
test_two.py ✓✓✓ 94% ███
test_two.py ✓✓✓✓ 100% ███
███████
==================================== warnings summary ===================================
c:\python37\lib\site-packages\_pytest\mark\structures.py:324
c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.run_these_cases - is this a typo? You can register custom mar
ks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
PytestUnknownMarkWarning,
-- Docs: https://docs.pytest.org/en/latest/warnings.html
Results (0.47s):
14 passed
3 failed
- test_asserts.py:33 test_add4
- test_asserts.py:64 test_true
- test_one.py:8 test_not_equal
테스트 실패 시 PDB를 사용하여 디버그 모드 활성화
PDB 실패 테스트 사례
--pdb
Python 표준 라이브러리의 디버깅 모듈입니다. pytest에서 –pdb 옵션을 사용하면 테스트가 실패할 때 대화형 디버깅 모드를 활성화할 수 있습니다.
(venv) E:\Programs\Python\Python_Pytest\TestScripts>pytest --pdb -v -x --lf
========================== test session starts =====================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0 -- c:\python37\python.exe
cachedir: .pytest_cache
rootdir: E:\Programs\Python\Python_Pytest\TestScripts, inifile: pytest.ini
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, nice-0.1.0, instafail-0.4.1, forked-1.0.2, emoji-0.2.0, allure-pytest-2.6.3
collected 119 items
run-last-failure: no previously failed tests, not deselecting items.
test_api_exceptions.py::TestAdd::test_missing_summary PASSED [ 0%]
test_api_exceptions.py::TestAdd::test_done_not_bool FAILED [ 1%]
>>>>>>>>>>>>>>>>>>> traceback >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
self = <TestScripts.test_api_exceptions.TestAdd object at 0x0000000CEEA430F0>
def test_done_not_bool(self):
"""Should raise an exception if done is not a bool."""
with pytest.raises(ValueError):
> tasks.add(Task(summary='summary', done='True'))
E Failed: DID NOT RAISE <class 'ValueError'>
test_api_exceptions.py:19: Failed
>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> e:\programs\python\python_pytest\testscripts\test_api_exceptions.py(19)test_done_not_bool()
-> tasks.add(Task(summary='summary', done='True'))
(Pdb)
--pdb
매개변수의 실행 결과를 추가한 후 마침내 (Pdb)의 대화형 모드에 진입한 것을 볼 수 있으며, (Pdb) 프롬프트가 나타나 pdb의 대화형 디버깅 기능을 사용할 수 있음을 나타냅니다.
매개변수 | 소개하다 |
---|---|
p/인쇄 표현 | expr의 값을 출력합니다. |
PP 특급 | 출력 expr의 값을 아름답게 합니다. |
l/목록 | 오류를 나열하고 오류 전후의 코드 5줄을 표시합니다. |
l/목록 시작, 끝 | 오류를 나열하고 줄 번호별로 표시할 코드 영역을 지정합니다. |
a/인수 | 현재 함수의 매개변수 목록을 인쇄합니다. (도우미 함수에서 중단점이 발생한 경우 유용합니다.) |
유/업 | 스택의 이전 레벨로 이동 |
d/아래 | 스택의 다음 레벨로 이동 |
q/종료 | 현재 디버깅 세션 종료 |
pdb 사용 방법에 대한 자세한 내용은 PDB 세부 사용법을 참조하세요 . |