繁体   English   中英

Django:测试帖子视图

[英]Django: Testing Post View

我正在尝试使用测试套件来测试我的帖子。 我一直在尝试按照文档进行操作。 我现在遇到的主要问题是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

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

位置:/ JSONChecker /

我正在使用Django 1.11和Python 2.7

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

暂无
暂无

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

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