python - Django test url always returns 404, but browsing the url with a browser is normal. What's going on?
淡淡烟草味
淡淡烟草味 2017-05-18 11:01:59
0
3
626

The test code is as follows:

from django.test import TestCase
# Create your tests here.

class SendviewsTestCase(TestCase):

    def test_get_data(self):
        rep = self.client.post('/data/datamsg/',{'data_name':'测试数据'})
        print (rep.content)
        # 测试http请求的返回码是否正确
        self.assertEqual(rep.status_code,200)
======================================================================
FAIL: test_get_data (dataforpro.tests.SendviewsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/yangtz/developproject/backend/dataforpro/tests.py", line 20, in test_get_data
    self.assertEqual(rep.status_code,200)
AssertionError: 404 != 200

----------------------------------------------------------------------
Ran 1 test in 0.017s

FAILED (failures=1)
Destroying test database for alias 'default'...

But when browsing the URL with a browser, it returns 200 normally. Is there something wrong with the setting?

淡淡烟草味
淡淡烟草味

reply all(3)
刘奇

It seems that your error is 404, which means that the url was not found when accessing /data/datamsg/. It depends on how your url address is configured. Try /data/datamsg and remove the / after it, or maybe it is what you want. Add http://localhost or something like that

曾经蜡笔没有小新

You said you can browse the url with a browser
When you access the url through the browser, the browser will send a GET request
but you used a POST request in the test.

So even if there is no connection between the browser being normal and the test passing or not

曾经蜡笔没有小新

404 appears, it should be as 1L said, the URL (host part) needs to be completed. Because when requesting through the browser, the URL in the page starts with /, and the browser will automatically add the host; and in the unit test case, you must write the complete URL yourself.
When 302 appears, you have to check: Which URL does 302 jump to? Where did you add any restrictions/validation conditions? For example, if "login_required" is added, if the system checks that there is no login, will it automatically jump to the login page with a 302?
There is another possibility. It should be 403. You forged a POST request, and Django has CSRF verification by default.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!