mongoengine connect() in settings.py testing problem

后端 未结 4 551
忘掉有多难
忘掉有多难 2021-02-06 04:29

I want to be able to do conditional connect() based on either I started django in testing mode or not.

in my settings.py I use mongoengine connect() method to connect to

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-06 05:22

    I'm not sure it's completely foolproof, but I use the fact that in a test, you will have probably started it from the command line with ./manage.py test, so 'test' is one of the command-line args. So this works:

    import sys
    if 'test' not in sys.argv:
        mongodb.connect()
    

提交回复
热议问题