unittest 라이브러리 사용

일반적으로 tests.py 에 작성

 

# myCalc.py
def add(a, b):
    return a + b
 
def substract(a, b):
    return a - b
# tests.py
import unittest
import myCalc
 
class MyCalcTest(unittest.TestCase):			# unittest.TestCase 상속
 
    def setUp(self):
    
    
    
    def tearDown(self):
    
   
   
    def test_add(self):
        c = myCalc.add(20, 10)
        self.assertEqual(c, 30)
 
    def test_substract(self):
        c = myCalc.substract(20, 10)
        self.assertEqual(c, 10)
 
if __name__ == '__main__':
    unittest.main()

 

1. setUp()

 - 테스트를 실시 전 사전작업을 하는 메서드

 ex) 데이터 연결

 

2. tearDown()

 - 테스트를 실시 후 사후작업을 하는 메서드

 - setup()이 성공적으로 실행 됐을 때만 실행 됨

 ex) 데이터 해제(clean up)

 

3. test_메서드명()

 - 테스트를 실시하는 함수

 - 테스트 함수는 반드시 test_ 로 시작해야 함

 

● 유닛테스트 시행

$ python -m unittest <test 할 모듈, 클래스, 메서드>

 

 

● assert메서드

assert 메서드 설명
assertEqual(a, b) a == b
assertIs(a, b) a is b
assertTrue(a) bool(a) is True
assertIsNone(a); a is None
assertIn(a, b) a in b
assertIsInstance(a, b) a 객체가 b의 인스턴스인지

 

 

 

 

 

 

● Django(장고)에서 Test 진행하는 법

- 각 App에 있는 test.py에서 코드 작성 후 진행

python manage.py test <AppName> 으로 tests.py 실행 가능

'Langauge > Python' 카테고리의 다른 글

[패키지] sqlalchemy  (0) 2021.02.16
인코딩, 디코딩(encoding, decoding)  (0) 2021.02.02
[라이브러리] logging (로깅, 로그)  (0) 2021.01.28
Pycharm 프로젝트 셋팅  (0) 2021.01.26
Python 라이브러리  (0) 2021.01.21

+ Recent posts