mercredi 13 juin 2018

Django View Testing: Can't pull context from client's post

I'm trying to make a testing suite for my view. All of my problems seem to stem from me not being able to access the context of my render that is returned from the view. I know the problem has something to do with DjangoTemplate backend but I have no idea what I am supposed to do to get response.context populated with the necessary variables. This is my tests.py code:

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'

This is what the render looks like:

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

I'm using Django 1.11 with python 2.7

Aucun commentaire:

Enregistrer un commentaire