@kyanny's blog

事実上すべての広告に見られる一貫したテーマとは、消費者の劣等性である - W・B・キイ「メディア・レイプ」

Django: how to run django unit tests without DB, in a single file

Sometimes useful to check how the assertion methods Django provides behave without creating a new Django project.

  • call settings.configure() at toplevel
  • call databases = [] at class level
  • call unittest.main() at the bottom


❯ python t.py -v
test_not_ok (__main__.InHTMLTestCase) ... FAIL
test_ok (__main__.InHTMLTestCase) ... ok
test_ok2 (__main__.InHTMLTestCase) ... ok

FAIL: test_not_ok (__main__.InHTMLTestCase)
Traceback (most recent call last):
  File "t.py", line 14, in test_not_ok
    self.assertInHTML('a', '<div>a<br>b</div>')
  File "/Users/kensuke.nagae/.anyenv/envs/pyenv/versions/3.6.10/lib/python3.6/site-packages/django/test/testcases.py", line 800, in assertInHTML
    self.assertTrue(real_count != 0, msg_prefix + "Couldn't find '%s' in response" % needle)
AssertionError: False is not true : Couldn't find 'a' in response

Ran 3 tests in 0.053s

FAILED (failures=1)