Django manage.py : Is it possible to pass command line argument (for unit testing)

前端 未结 5 2205
Happy的楠姐
Happy的楠姐 2021-02-18 13:46

Is it possible to pass command line arguments to Django\'s manage.py script, specifically for unit tests? i.e. if I do something like

manage.py test         


        
5条回答
  •  臣服心动
    2021-02-18 14:12

    As an alternative way to manage.py test -a do_this you can use specific settings file

    manage.py --settings=project.test_settings test
    

    and define in this file whatever you want.

    # test_setting.py
    SPECIFIC_OPTION = "test"
    
    # tests.py
    from django.conf import settings
    ...
    def setUp(self):
        if settings.SPECIFIC_OPTION:
            ....
    

    If you need really dynamic options, maybe you can use sys.argv in test_settings.py, but it is a really dirty hack.

提交回复
热议问题