Visual Studio Test Explorer For Python Django Test

2019-07-07 17:03发布

问题:

Starting test runs from Visual Studio Test Explorer doesn't call database creation and Django setup() before running the test(s).

My current workaround is to call something like this, which I keep in config.settings.test

def setUpTestingWithDatabase():
    import os
    from django.test.utils import setup_test_environment
    from django import setup
    os.environ["DJANGO_SETTINGS_MODULE"] = "config.settings.test"
    setup_test_environment()
    setup()

by adding this code to the top of every test file:

import sys
if not ('manage.py' in sys.argv):
    from config.settings.test import setUpTestingWithDatabase
    setUpTestingWithDatabase()

This works, and can still use the nose runner, and a CLI call like

> python manage.py test myApp

This works all very well, but I would like to avoid this code, keep it vanilla AND use it from both CLI and Test Explorer. MS Visual Studio supports runsettings, and I wonder if they can be used? Any other ideas?