TDD
[TDD with Python] - 2
kyeongjun-dev
2021. 5. 17. 15:26
참고한 사이트
https://ugaemi.com/tdd/Django-server-test/
감사합니다
https://not-to-be-reset.tistory.com/422
이 게시글에서 사용한 코드를 응용(Firefox 사용)
unit 테스트
- unit_tests.py
from selenium import webdriver
import unittest
class DjangoTest(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
def tearDown(self):
self.browser.quit()
def test_check_title(self):
self.browser.get('http://localhost:8000')
self.assertIn('Django', self.browser.title)
if __name__ == '__main__':
unittest.main()
unittest.TestCase를 상속하는 클래스 DjangoTest 생성.
setUp()과 tearDown()은 각 테스트 시작 전과 후에 실행되는 메소드로 setUp은 브라우저를 열 때, tearDown은 닫을 때 사용. assertIn(a, b) 메소드는 a in b를 확인하는 메소드. 위 코드로 Django라는 텍스트가 브라우저의 타이틀에 있는지 확인가능.
- 실행 결과(일부러 실패)
$ python unit_tests.py
F
======================================================================
FAIL: test_check_title (__main__.DjangoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "unit_tests.py", line 13, in test_check_title
self.assertIn('Django', self.browser.title)
AssertionError: 'Django' not found in 'The install worked successfully! Congratulations!'
----------------------------------------------------------------------
Ran 1 test in 4.742s
FAILED (failures=1)
- 실행 결과(성공)
$ python unit_tests.py
.
----------------------------------------------------------------------
Ran 1 test in 6.806s
OK