Programming/Unittest
Unittest
c29130811
2024. 9. 8. 13:05
Python 내장 모듈로, 단위 테스트를 위한 프레임워크다. Junit에서 영감을 받아 비슷한 특징을 갖고 있다.
테스트 케이스 작성, 테스트 스위트 구성, 테스트 실행 (assert 검증), 테스트 결과 보고 등의 다양한 기능을 제공한다.
import unittest
# 테스트할 함수
def add(x, y):
return x + y
# 테스트 케이스 정의
class TestOperation(unittest.TestCase):
def test_add(self):
result = add(10, 5)
self.assertEqual(result, 15) # 기대 값과 실제 값이 같은지 확인
# 테스트 실행
if __name__ == '__main__':
unittest.main()
실행은 단순히 python test_1.py 하면 Ran 1 test 하고 결과가 나온다.
june@juneui-MacBookPro pytest % python test_1.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
june@juneui-MacBookPro pytest %
만약 실패 할 경우
june@juneui-MacBookPro pytest % python test_1.py
F
======================================================================
FAIL: test_add (__main__.TestMathOperations.test_add)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/june/Documents/Study/pytest/test_1.py", line 33, in test_add
self.assertEqual(result, 19) # 기대 값과 실제 값이 같은지 확인
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 15 != 19
----------------------------------------------------------------------
Ran 1 test in 0.060s
FAILED (failures=1)
june@juneui-MacBookPro pytest %
요런 식으로 FAILED로 나온다.
https://docs.python.org/ko/3/library/unittest.html
unittest — Unit testing framework
Source code: Lib/unittest/__init__.py(If you are already familiar with the basic concepts of testing, you might want to skip to the list of assert methods.) The unittest unit testing framework was ...
docs.python.org
728x90