简体   繁体   English

Django:测试帖子视图

[英]Django: Testing Post View

I'm trying to test my post with a testing suite. 我正在尝试使用测试套件来测试我的帖子。 I've just been trying to follow the documentation to do this. 我一直在尝试按照文档进行操作。 The main problem I'm having right now is that response.context is returning None. 我现在遇到的主要问题是response.context返回None。 This is what my test class looks like: 这是我的测试类的样子:

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'

Also this is what my render looks like: 这也是我的渲染的样子:

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

If the form is invalid or the extension isn't json this is what the render looks like (this shouldn't be triggered by suite): 如果表单无效或扩展名不是json,则呈现器的外观如下(不应由套件触发):

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

Content-Length: 0 内容长度:0

Content-Type: text/html; 内容类型:text / html; charset=utf-8 字符集= utf-8

Location: /JSONChecker/ 位置:/ JSONChecker /

I'm using Django 1.11 and Python 2.7 我正在使用Django 1.11和Python 2.7

仅在使用DjangoTemplates后端时填充Context属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM