Django testing
파이썬 TestCase, unit test 사용하기
wangkisa
2021. 5. 31. 11:58
1. Django TestCase
Django에서 기본으로 제공하는 TestCase 클래스
django.test 에 포함된 클래스이며, TransctionTestCase 를 상속받아서 사용하고 있다.
데이터베이스를 사용하지 않는 경우는 SimpleTestCase 사용하는 것을 권장한다.
- classmethod TestCase.setUpTestData()
- 초기에 한번만 실행해서 초기 데이터 를 만드는 역할
- setUp()
- test 할때마다 호출되서 해당 데이터셋을 만드는 역할
class YourTestClass(TestCase):
@classmethod
def setUpTestData(cls):
Author.objects.create(first_name='test setUpTestData', last_name='once')
pass
def setUp(self):
Author.objects.create(first_name='test setup', last_name='once per call')
pass
def test_false_is_false(self):
print("Method: test_false_is_false.")
self.assertFalse(False)
def test_false_is_true(self):
print("Method: test_false_is_true.")
self.assertTrue(True)
def test_one_plus_one_equals_two(self):
print("Method: test_one_plus_one_equals_two.")
self.assertEqual(1 + 1, 2)
python manage.py test 명령으로 실행해보면 다음과 같은 결과를 확인해 볼 수 있다.
테스트에 대해 더 많은 정보를 출력하기
만약 더 많은 정보를 출력하기 위해서는 다음과 같은 명령을 사용한다.
python manage.py test --verbosity 2
예를 들어 테스트 실패와 더불어 성공 (그리고 테스트 데이터베이스 생성 과정에 대한 정보)를 나열하려면 다음과 같이 verbosity를 "2"로 설정할 수 있다.
허용되는 verbosity levels 은 0, 1, 2, and 3, 이며 기본값은 "1"
참고. unittest의 assert메소드 리스트
Method | Checks that | New in |
assertEqual(a, b) | a == b | |
assertNotEqual(a, b) | a != b | |
assertTrue(x) | bool(x) is True | |
assertFalse(x) | bool(x) is False | |
assertIs(a, b) | a is b | 3.1 |
assertIsNot(a, b) | a is not b | 3.1 |
assertIsNone(x) | x is None | 3.1 |
assertIsNotNone(x) | x is not None | 3.1 |
assertIn(a, b) | a in b | 3.1 |
assertNotIn(a, b) | a not in b | 3.1 |
assertIsInstance(a, b) | isinstance(a, b) | 3.2 |
assertNotIsInstance(a, b) | not isinstance(a, b) | 3.2 |
assertRaises(exc, fun, *args, **kwds) | fun(*args, **kwds) raises exc | |
assertRaisesRegex(exc, r, fun, *args, **kwds) | fun(*args, **kwds) raises exc and the message matches regex r | 3.1 |
assertWarns(warn, fun, *args, **kwds) | fun(*args, **kwds) raises warn | 3.2 |
assertWarnsRegex(warn, r, fun, *args, **kwds) | fun(*args, **kwds) raises warn and the message matches regex r | 3.2 |
assertLogs(logger, level) | The with block logs on logger with minimum level | 3.4 |
assertAlmostEqual(a, b) | round(a-b, 7) == 0 | |
assertNotAlmostEqual(a, b) | round(a-b, 7) != 0 | |
assertGreater(a, b) | a > b | 3.1 |
assertGreaterEqual(a, b) | a >= b | 3.1 |
assertLess(a, b) | a < b | 3.1 |
assertLessEqual(a, b) | a <= b | 3.1 |
assertRegex(s, r) | r.search(s) | 3.1 |
assertNotRegex(s, r) | not r.search(s) | 3.2 |
assertCountEqual(a, b) | a and b have the same elements in the same number, regardless of their order | 3.2 |
assertMultiLineEqual(a, b) | strings | 3.1 |
assertSequenceEqual(a, b) | sequences | 3.1 |
assertListEqual(a, b) | lists | 3.1 |
assertTupleEqual(a, b) | tuples | 3.1 |
assertSetEqual(a, b) | sets or frozensets | 3.1 |
assertDictEqual(a, b) | dicts | 3.1 |
참조 사이트
- Django 튜토리얼 파트 문서 - https://developer.mozilla.org/ko/docs/Learn/Server-side/Django/Testing
- 파이썬 - 기본을 갈고 닦자! - https://wikidocs.net/16107