Python Django:测试后视图

Python Django:测试后视图,python,django,file,testing,post,Python,Django,File,Testing,Post,我正在尝试用测试套件测试我的帖子。我只是一直在试着按照规则来做这件事。我现在遇到的主要问题是response.context返回None。 这就是我的测试类的外观: class JSONHandlerTester(TestCase): def setUp(self): self.client = Client() self.jsonTestPath = os.path.join(settings.MEDIA_ROOT,'json','jsonTests') def testi

我正在尝试用测试套件测试我的帖子。我只是一直在试着按照规则来做这件事。我现在遇到的主要问题是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})
这也是我的渲染的外观:

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,则呈现就是这样的(不应由套件触发):

内容长度:0

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

位置:/JSONChecker/


我使用的是Django 1.11和Python 2.7,只有在使用Django模板后端时才会填充上下文属性。

我必须从后端提取上下文吗?我尝试使用context_数据,但我得到一个错误,说它不存在。如何填充context属性,以便提取想要提取的变量?