我有一个模型,我将电子邮件和SMS发送给post_save
信号中的用户,我正在多次创建该模型,因此它多次发送电子邮件和SMS。
我打算为测试SMS和电子邮件编写新测试。
def send_activation_mail_sms(sender, instance, created, **kwargs):
if created :
mobile_activation = UserMobileActivation.objects.create(user=instance,randomword=randomword(50),ref=ref)
email_activation = UserEmailActivation.objects.create(user=instance,randomword=randomword(50),ref=ref)
url_email = "{0}view/v1/email/activation/{1}/".format(HOSTNAME,email_activation.randomword) short_url_email = url_shortener(long_url_email)
url_sms = "{0}view/v1/mobile/activation/{1}".format(HOSTNAME,mobile_activation.randomword)
app.send_task("apps.tasks.send_sms",
args=[TEXTLOCAL_APIKEY,mobile_activation.stockuser.user.username ,'TXTLCL','Activate your mobile here {0}'.format(url_sms)])
app.send_task("apps.tasks.send_email",
args=[email_activation.user.user.email, EMAIL_VERIFICATION_SUBJECT,
EMAIL_VERIFICATION_TEMPLATE, {"host": HOSTNAME, "verify_email_url": url_email}])
我正在通过post_save
信号中的created
ARG传递,我可以在此处通过额外的ARG,以便在执行python manage.py test
时会跳过发送短信和电子邮件。我使用以一种方式使用版本来进行测试的API的不同版本,但是由于没有request
来到这里,所以我无法在这里捕获request.version
。请建议。
最初在您的设置中设置一些变量。
# settings.py
MY_ENV = "DEVELOPMENT"
然后,基于 MY_ENV
from django.conf import settings
def send_activation_mail_sms(sender, instance, created, **kwargs):
if created and settings.MY_ENV == "DEVELOPMENT":
# do your stuff
django为我们提供了在测试过程中覆盖设置配置的,请参见doc Override设置。因此,您可以在测试本身中覆盖 MY_ENV
值