Python 테스트 입문
Alexander Levin
Data Scientist
일반적인 문제:
이런 문제들은 수정 비용의 큰 증가로 이어질 수 있습니다.
테스팅은 다음에 도움이 됩니다:
요구사항을 충족하는지 보장하려면 테스팅이 필요합니다.
테스트 - 소프트웨어나 시스템의 정확성을 검증하는 절차
assert 문비행기를 떠올려 보십시오:
위 모든 것이 테스트이며, 안전을 위해 필요합니다.

assert condition - condition이 True인지 검사합니다.condition이 False이면 AssertionError가 발생합니다.pytest - 파이썬의 대표적인 테스트 프레임워크로, 테스트를 쉽게 작성할 수 있습니다.
파이썬에서 pytest로 작성한 "assert" 테스트 예:
import pytest
# A function to test
def squared(number):
return number * number
# A test function always starts with "test"
def test_squared():
assert squared(-2) == squared(2)
with 문으로 사용하는 파이썬 객체# 파일 쓰기 예시
with open("hello_world.txt", 'w') as hello_file:
hello_file.write("Hello world \n")
pytest.raises - 테스트에서 Exception 발생을 예상할 때 사용합니다.
import pytest
# A function to test
def division(a, b):
return a / b
# A test function
def test_raises():
with pytest.raises(ZeroDivisionError):
division(a=25, b=0)
테스트란:
테스트 구현:
pytest - 테스트를 단순화하는 강력한 파이썬 프레임워크assert - 조건을 검증해 기본 테스트를 만드는 파이썬 키워드pytest.raises - Exception 발생이 예상되는 테스트에 쓰는 컨텍스트 매니저Python 테스트 입문