코드로 우주평화

pytest _ pytest-cov로 coverage 확인하기(실습) 본문

나는 이렇게 학습한다/Debug

pytest _ pytest-cov로 coverage 확인하기(실습)

daco2020 2022. 9. 5. 23:55
 

GitHub - Daco2020/pytest-cov-practice: Repository for practicing pytest-cov

Repository for practicing pytest-cov. Contribute to Daco2020/pytest-cov-practice development by creating an account on GitHub.

github.com

(실습에 사용하는 소스코드는 위 링크에서 확인할 수 있습니다.)

 

 

 

pytest-cov 란?

pytest-cov--cov 옵션 추가하여 테스트 대상의 coverage를 확인할 수 있습니다. 사용자는 coverage 수치를 통해 테스트의 적용범위를 파악할 수 있습니다.

 

 

실습 방법

레포지토리 복사

git clone https://github.com/Daco2020/pytest-cov-practice.git

 

라이브러리 설치

pip install pytest-cov

 

실행 명령어

pytest --cov

또는

pytest --cov=[src] [test]
  • [src] 위치에 대상을 명시하면 대상에 대해서만 Cover를 확인할 수 있습니다.
  • [test] 위치에 경로를 입력하면 해당 경로의 테스트만 수행합니다.




명령어 예시

pytest-cov 는 명령어 조합에 따라 다음과 같은 경우의 수를 가집니다. 소스코드를 클론 받아 따라해보세요.

 

1.  pytest --cov 는 모든 테스트를 실행하고, 대상 전체의 Cover를 보여줍니다.

# 실행

pytest --cov

 

# 결과

collected 4 items

tests/test_main.py .. 
tests/test_serve.py ..  

---------- coverage: platform darwin, python 3.10.6-final-0 ----------
Name                  Stmts   Miss  Cover
-----------------------------------------
main/file.py              6      0   100%
serve/file.py             6      0   100%
tests/__init__.py         0      0   100%
tests/test_main.py        5      0   100%
tests/test_serve.py       5      0   100%
-----------------------------------------
TOTAL                    22      0   100%



2. pytest --cov=main 는 모든 테스트를 실행하고, main에 대해서만 Cover를 보여줍니다.

# 실행

pytest --cov=main
# 결과

collected 4 items

tests/test_main.py ..
tests/test_serve.py ..

---------- coverage: platform darwin, python 3.10.6-final-0 ----------
Name           Stmts   Miss  Cover
----------------------------------
main/file.py       6      0   100%
----------------------------------
TOTAL              6      0   100%



3. pytest --cov=main tests/test_main.py 는 test_main.py의 테스트만 실행하고, main에 대해서만 Cover를 보여줍니다.

# 실행

pytest --cov=main tests/test_main.py
# 결과

collected 2 items                          

tests/test_main.py ..

---------- coverage: platform darwin, python 3.10.6-final-0 ----------
Name           Stmts   Miss  Cover
----------------------------------
main/file.py       6      0   100%
----------------------------------
TOTAL              6      0   100%

 

 

 

pytest-cov에 대해 더 자세한 내용을 알고 싶다면 공식문서를 확인해주세요.

 

 


 

Missing 항목 추가

--cov-report term-missing

해당 명령어를 추가하면 Missing 항목을 추가할 수 있습니다.

 

 

사용 예

pytest --cov-report term-missing
Name                  Stmts   Miss  Cover   Missing
---------------------------------------------------
------------.py         10      0   100%
------------.py         56      1    98%   57
------------.py         32      1    97%   159
------------.py         41      0   100%
------------.py         23      0   100%
------------.py         122     6    95%   134-143, 153, 172, 304
------------.py         26      0   100%
------------.py         15      0   100%
---------------------------------------------------
TOTAL                   325      8    98%
  • Stmts - 명령문 수
  • Miss - 실행되지 않은 명령문 수
  • Cover - 테스트 커버리지 수치
  • Missing - Miss 명령문의 줄 번호