title: How to use multiple databases for unit test cases in django pytest
body: <p>I have been working on a django application which uses multiples databases through database router like the following. I want to write unit test cases for the apis developed using Django Rest Framework.</p>
  <pre><code>class MultiDbRouter(object):
      &quot;&quot;&quot;
          Router for handling multiple database connections
          based on the db configs in the request object
      &quot;&quot;&quot;
  
      def _multi_db(self):
          if hasattr(request_cfg, 'db'):
              if request_cfg.db in settings.DATABASES:
                  return request_cfg.db
              else:
                  raise Http404
          else:
              return 'default'
  
      def db_for_read(self, model, **hints):
          &quot;&quot;&quot;
              Set database for reading based on request config
          &quot;&quot;&quot;
          if model._meta.app_label == 'survey':
              return 'survey'
          if model._meta.app_label != 'analytics':
              return 'default'
          return self._multi_db()
  
      def db_for_write(self, model, **hints):
          &quot;&quot;&quot;
              Set database for writing based on request config
          &quot;&quot;&quot;
          if model._meta.app_label == 'survey':
              return 'survey'
          if model._meta.app_label != 'analytics':
              return 'default'
          return self._multi_db()
  
      def allow_relation(self, obj1, obj2, **hints):
          &quot;&quot;&quot;
              allow relation between objects
          &quot;&quot;&quot;
          return True
  
      def allow_syncdb(self, db, model):
          &quot;&quot;&quot;
              allow syncing of database
          &quot;&quot;&quot;
          return True
  
  </code></pre>
  <p>I have started using pytest for writing the testcases, but as I read the docs of pytest-django library, it says that &quot;Currently pytest-django does not specifically support Django’s multi-database support.&quot;</p>
  <p>Is there any way to workaround this limitation? Or should I use some other library other than pytest-django to write the unit test cases? If yes, which one?</p>
  <p>What is the ideal way of going forward?</p>
  <p>Thanks in advance!</p>