====== Django Rest Framework Testing Tools ====== Django Rest Framework에서 API 테스팅을 수행하기 위해 testing tool에 대해 조사한다. ===== Python unittest ===== ==== TestCase Class ==== 우선 가장 먼저 python 표준 라이브러리인 unittest를 알아본다. Rest framework, django 모두 unittest를 상속받는 것부터 시작한다. 가장 기본적인 소스 코드인 https://docs.python.org/2.7/library/unittest.html#basic-example 페이지를 보자. 다음은 unittest의 한 클래스인 'TestCase'의 중요 함수들이다. * def setUp() * def tearDown() * def test_*() * def setUpClass() * def tearDownClass() 'test_*()' 류의 함수는 각 테스트 케이스(test case)를 담당한다. setUp(), tearDown()은 각 테스트 케이스마다 필요한 것들을 준비하는 픽스쳐(fixture)에 해당한다. 그러므로 TestCase 클래스 하나는 대략적으로 테스트 수트(test suite)에 해당한다. ==== Organizing test codes ==== 간단히 말해 TestSuite 클래스에서 코드 테스트를 관리하도록 한다. TestCase를 서브클래싱해서 자신만의 테스트 코드를 만들고, runTest() 함수를 오버라이딩하여 원하는 동작을 하게 만들 수 있다. 이 때 tearDown(), setUp() 함수를 별도로 만들 수 있다. 여러 서브클래스에서 같은 동작을 반복해야 할 경우 반복할 동작에 대한 코드를 미리 상위 클래스로 선언하고 테스트 코드를 담은 각각의 클래스들은 그렇게 만들어진 코드를 상속받아 쓰면 된다. ===== Django Code Testing ===== ===== 외부 페이지 ===== * Django Rest Framework Testing [[http://www.django-rest-framework.org/api-guide/testing]] * Testing in Django [[https://docs.djangoproject.com/en/1.7/topics/testing/]] * Writing and running tests [[https://docs.djangoproject.com/en/1.7/topics/testing/overview/]] * Testing tools [[https://docs.djangoproject.com/en/1.7/topics/testing/tools/]] * Advanced testing topics [[https://docs.djangoproject.com/en/1.7/topics/testing/advanced/]] * Python 2.7 unittest [[https://docs.python.org/2.7/library/unittest.html#module-unittest]]