检测django测试模式 [英] Detect django testing mode

查看:112
本文介绍了检测django测试模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个可重用的django应用程序,我需要确保它的模型仅在应用程序处于测试模式时才同步。我试图使用一个自定义DjangoTestRunner,但是我没有发现如何做到这一点的例子(该文档仅显示如何定义一个自定义测试运行器)。



所以有没有人知道如何做?



编辑



这是我如何做:

  #in settings.py 
import sys
TEST ='测试'在sys.argv

希望它有帮助。

解决方案

我认为这里提供的答案是 https://stackoverflow.com/a/ 7651002/465673 是一个更干净的方法:


将其放在您的settings.py中:




  import sys 

TESTING = sys.argv [1:2] == ['test']


I'm writing a reusable django app and I need to ensure that its models are only sync'ed when the app is in test mode. I've tried to use a custom DjangoTestRunner, but I found no examples of how to do that (the documentation only shows how to define a custom test runner).

So, does anybody have an idea of how to do it?

EDIT

Here's how I'm doing it:

#in settings.py
import sys
TEST = 'test' in sys.argv

Hope it helps.

解决方案

I think the answer provided here https://stackoverflow.com/a/7651002/465673 is a much cleaner way of doing it:

Put this in your settings.py:

import sys

TESTING = sys.argv[1:2] == ['test']

这篇关于检测django测试模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆