在python / django中测试200代码

时间:2016-10-07 08:19:30

标签: python django testing cookies

我正在尝试测试我的项目,并希望通过以下测试获得200状态代码:

import unittest
from django.test import Client
from views2 import *
import requests


class TestCharts(unittest.TestCase):

    def test_200(self):

        url_csrftoken = 'http://127.0.0.1:8000/login/'
        url = 'http://127.0.0.1:8000/'
        url_login = 'http://127.0.0.1:8000/login/signin'
        client = requests.session()
        client.get(url_csrftoken)
        csrftoken = client.cookies['csrftoken']
        login_data = dict(inputUser='admin', inputPassword='mypass', csrfmiddlewaretoken=csrftoken, next=url)
        client.post(url_login, data=login_data)
        r = client.get(url)
        self.assertEqual(r.status_code,200)

if __name__ == '__main__':


    print "Testing"
    unittest.main()

停在:client.get(url_csrftoken)并发出以下错误:

Traceback (most recent call last):
  File "tests2.py", line 115, in test_networking
    csrftoken = client.cookies['csrftoken']
  File "/home/user/project/env/lib/python2.7/site-packages/requests/cookies.py", line 327, in __getitem__
    return self._find_no_duplicates(name)
  File "/home/user/project/env/lib/python2.7/site-packages/requests/cookies.py", line 398, in _find_no_duplicates
    raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))
KeyError: "name='csrftoken', domain=None, path=None"

但我并不知道发生了什么。

0 个答案:

没有答案