【问题标题】:Django: Testing Post ViewDjango:测试帖子视图
【发布时间】:2018-06-12 23:45:49
【问题描述】:

我正在尝试使用测试套件测试我的帖子。我一直在尝试关注documentation 来做到这一点。我现在遇到的主要问题是 response.context 返回 None。 这是我的测试类的样子:

class JSONHandlerTester(TestCase):
def setUp(self):
    self.client = Client()
    self.jsonTestPath = os.path.join(settings.MEDIA_ROOT,'json','jsonTests')


def testing(self):
    for test in os.listdir(self.jsonTestPath):
        testFile = os.path.join(os.path.join(self.jsonTestPath),test)
        split = test.split('.')
        testName = split[0]
        testNameArray = re.findall('[a-zA-z][^A-Z]*', testName)
        project = testNameArray[0]
        team = testNameArray[1]
        with open(testFile) as json:
            response = self.client.post('/JSONChecker', {'json_project': project, 'json_team': team, 'json': json})
        print response
        print response.context
        if (response.context['title'] == "Congratulations!!! Your JSON Passes!!!" and testNameArray[2] == "Pass") or (response.context['title'][2:] == "The" and testNameArray[2] == "Fail"):
           print test+': Works'
        else:
           print test+': BREAKS: PROBLEM DETECTED'

这也是我的渲染的样子:

return render(request, 'JSONChecker.html',context = {'title': title, 'validationErrors':validationErrors,'errors':errors, 'isLoggedIn':isLoggedIn, 'form': form, 'post':post})

如果表单无效或扩展名不是 json,这就是渲染的样子(这不应该由套件触发):

return render(
            request,
            'JSONChecker.html',
            context = {'title': title,'errors': errors,'isLoggedIn':isLoggedIn,'team':team, 'email':email,'form':form, 'post': post},
        )

内容长度:0

内容类型:文本/html;字符集=utf-8

位置:/JSONChecker/

我正在使用 Django 1.11 和 Python 2.7

【问题讨论】:

    标签: python django file testing post


    【解决方案1】:

    只有在使用 DjangoTemplates 后端时才会填充上下文属性。

    【讨论】:

    • 我必须从后端提取上下文吗?我尝试使用 context_data 但我得到一个错误,说它不存在。我如何能够填充上下文属性,以便我可以拉出我想要拉出的变量?
    猜你喜欢
    • 2019-05-20
    • 2015-12-19
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    相关资源
    最近更新 更多