Django:测试Post View

时间:2018-06-12 23:45:50

标签: python django file testing post

我正在尝试使用测试套件测试我的帖子。我一直试图按照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

Content-Type:text / html;字符集= UTF-8

位置:/ JSONChecker /

我正在使用Django 1.11和Python 2.7

1 个答案:

答案 0 :(得分:0)

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