Python
내 코드를 테스트한다. (feat. pytest)
Dan-k
2022. 3. 28. 19:55
반응형
개발을 하다보면 내가 짠 코드가 의도에 맞게 잘 작동하는지를 한번씩 점검할 필요가 있다.
개발하는 파일마다 그때그때 테스트를 하면서 완료할 수도 있지만, 대규모의 프로젝트로 .py\
파일이 많아진다면 테스트가 조금 성가실 수 있다.
pytest를 이용하면, 접두사 test_ .py
or 접미사 _test.py
가 붙은 파일들을 자동으로 가지고와 해당 파일을 실행하고, 실행된 결과에 대해 확인할 수 있다.
# content of test_sample.py
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 5
pytest를 실행시키면, 아래와 같은 결과가 나온다.
$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-7.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
collected 1 item
test_sample.py F [100%]
================================= FAILURES =================================
_______________________________ test_answer ________________________________
def test_answer():
> assert inc(3) == 5
E assert 4 == 5
E + where 4 = inc(3)
test_sample.py:6: AssertionError
========================= short test summary info ==========================
FAILED test_sample.py::test_answer - assert 4 == 5
============================ 1 failed in 0.12s =============================
728x90
반응형
LIST