1

Is it possible not to use a unit test tool provided by Django but use native python unittest library?

I know that for simple tests it works just fine. But when i involve other apps and models into the test i get various exceptions like 'apps are not loaded yet and so on'.

So is there a workaround to test django project with unittest lib that works in most cases ?

Laimonas Sutkus
  • 3,247
  • 2
  • 26
  • 47
  • but why would you want to test Django code without using Django test features? I mean, they were implemented to pave the way when comes to test code for Django. Did you check other Django test classes options, like [TransactionTestCase](https://docs.djangoproject.com/en/dev/topics/testing/tools/#transactiontestcase) or [SimpleTestCase](https://docs.djangoproject.com/en/dev/topics/testing/tools/#simpletestcase)? – Jonatas CD Apr 03 '18 at 15:33
  • Simple answer - with django tests i can not test individual single tests, only classes that contains tests. This is the case when developing with pycharm IDE. The integration of unittests is better. – Laimonas Sutkus Apr 09 '18 at 20:42
  • Plus there are some incovenient test databse creatinios where if you want to omit this functionality you have to specify your own test runners. The list goes on. – Laimonas Sutkus Apr 09 '18 at 20:45

1 Answers1

0

You could initialize Django manually in your test module, which should mean you can interact with your models - for example:

import os
from unittest import TestCase

os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
import django
django.setup()

# Model imports, test code

You may have your reasons for doing this, although as Jonatas CD states in his comment, Django's own test classes would really be the best approach as they provide lots of useful features.

Will Keeling
  • 22,055
  • 4
  • 51
  • 61