1

I'm using Python 3.8 and Django 3. I have the following directory structure ...

- manage.py  
+ tests
    - __init__.py  
    - factories.py  
    - test_models.py    
    - test_serializers.py
+ directory
    - test_settings.py

I launch my tests using

python manage.py test --settings=directory.test_settings

Below are teh contents of my "test_settings.py" file ...

from .settings import *

class DisableMigrations(object):
    # ref: https://stackoverflow.com/a/28560805/12578202

    def __contains__(self, item):
        return True

    def __getitem__(self, item):
        return None

How do I configure my test settings file above to be default so that I can run tests simply by doing

python manage.py test
Dave
  • 15,639
  • 133
  • 442
  • 830
  • You can create your own test command which extends the default test command class, and uses the test settings. – James Lin Aug 04 '20 at 01:52
  • Thanks, but do you know is there a more conventional way to do it? Kind of like how "pytest" has a "conftest.py"? – Dave Aug 04 '20 at 12:30
  • No not that I am aware of, another way is to modify your `manage.py` then switch to your test_settings.py when command argument is `test` – James Lin Aug 04 '20 at 21:44

1 Answers1

0

Not that I am aware of a conventional way to do it, but here is a workaround by editing your manage.py

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == 'test':
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "directory.test_settings")
    else:
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourproject.settings")

...
James Lin
  • 25,028
  • 36
  • 133
  • 233