我正在尝试为我的观点制作测试套件。我的所有问题似乎都源于我无法访问从视图返回的渲染的上下文。我知道这个问题与DjangoTemplate后端有关,但是我不知道我应该做些什么来让response.context填充必要的变量。这是我的tests.py代码:
class JSONHandlerTester(TestCase):
def setUp(self):
self.client = Client()
self.jsonTestPath = os.path.join(settings.MEDIA_ROOT,'json','jsonTests')
pass
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})
编辑:当我运行测试时,我得到TypeError: NonType object has no attribute.
它在我的if语句的行上。我打印出response.context
,发现它打印出None
。这就是我知道问题来自于我无法从渲染中的dict中提取上下文变量。
我正在Django 1.11
使用Python 2.7