django unit tests without a db

前端 未结 11 1510
醉梦人生
醉梦人生 2020-11-29 16:11

Is there a possibility to write django unittests without setting up a db? I want to test business logic which doesn\'t require the db to set up. And while it is fast to setu

11条回答
  •  半阙折子戏
    2020-11-29 16:49

    From django.test.simple

      warnings.warn(
          "The django.test.simple module and DjangoTestSuiteRunner are deprecated; "
          "use django.test.runner.DiscoverRunner instead.",
          RemovedInDjango18Warning)
    

    So override DiscoverRunner instead of DjangoTestSuiteRunner.

     from django.test.runner import DiscoverRunner
    
     class NoDbTestRunner(DiscoverRunner):
       """ A test runner to test without database creation/deletion """
    
       def setup_databases(self, **kwargs):
         pass
    
       def teardown_databases(self, old_config, **kwargs):
         pass
    

    Use like that :

    python manage.py test app --testrunner=app.filename.NoDbTestRunner
    

提交回复
热议问题