python - 在单元测试中使用 django.template 上下文时遇到问题

标签 python django unit-testing django-1.3

当我使用 django.template 中的 Context 时,这里出现了一些困惑的情况。

以下在 python shell 中工作:

>>> from django.template import Context, Template
>>> b=Template('TEST').render(Context())
>>> print b
TEST

当我在单元测试中使用完全相同的代码时,出现以下错误:

Traceback (most recent call last):
  File "/newsletterapi/tests.py", line 25, in setUp
    b = Template('TEST').render(Context())
  File "/opt/python2.7/lib/python2.7/site-packages/django/template/base.py", line 121, in render
    context.render_context.push()
AttributeError: 'Context' object has no attribute 'render_context'

单元测试看起来像这样:

from django.test import TestCase
from myproject.newsletterapi.models import Newsletter
from django.utils.termcolors import colorize
from django.db import IntegrityError
from django.template import Template, Context
import random
import datetime
from decimal import *
import string


class NewsletterTest(TestCase):

    def setUp(self):
        b = Template('TEST').render(Context()) # this is line 25
        self.newsletter = Newsletter(body=b)
        self.newsletter.save()

### ... continues here

有谁知道为什么这在 shell 中有效但在单元测试中无效?我感谢每一个提示。

最佳答案

好的,我得到了解决方案:

from decimal import *

是“坏的”这个库也有一个 Context 对象。 感谢任何人阅读!

关于python - 在单元测试中使用 django.template 上下文时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10031945/

相关文章:

python - 使用递归将 list 中的列表转换为仅包含整数的列表

Python/Boto + AWS S3 版本控制 : how do I replace the current key with older version?

python - 如果进程消耗过多内存,如何暂停进程?

django - 在 Django 中注册后重定向到上一页

unit-testing - 如何模拟 MSMQ 消息队列

python - 通过 Python 实现 Spark 和 Cassandra

python - django-admin startproject 不能在 OS X 上使用 python3

Python (Windows Server) Virtualenv 无法识别 Django 已安装

unit-testing - 在go测试中模拟嵌入式结构

unit-testing - 单元测试指南