gpt4 book ai didi

python-3.x - 通过 Pytest 和 Requests 测试 Django 时出现 MissingSchema 错误

转载 作者:行者123 更新时间:2023-12-04 16:45:33 28 4
gpt4 key购买 nike

尝试通过 python-requests 制作简单的测试管理页面

import requests
from django.urls import reverse


def test_admin():
resp = requests.get(reverse('admin:index'))
assert resp.status_code == 200

但出现意外错误
    def test_admin():
> resp = requests.get(reverse('admin:index'))

src/users/tests/test_user_admin.py:6:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
venv1/lib/python3.5/site-packages/requests/api.py:72: in get
return request('get', url, params=params, **kwargs)
venv1/lib/python3.5/site-packages/requests/api.py:58: in request
return session.request(method=method, url=url, **kwargs)
venv1/lib/python3.5/site-packages/requests/sessions.py:494: in request
prep = self.prepare_request(req)
venv1/lib/python3.5/site-packages/requests/sessions.py:437: in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
venv1/lib/python3.5/site-packages/requests/models.py:305: in prepare
self.prepare_url(url, params)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

> raise MissingSchema(error)
E requests.exceptions.MissingSchema: Invalid URL '/admin/': No schema supplied. Perhaps you meant http:///admin/?

venv1/lib/python3.5/site-packages/requests/models.py:379: MissingSchema

已安装的软件包列表
pytest==3.3.2
requests==2.18.4
pytest-django==3.1.2
django==1.10.2

阅读了很多文档和手册,但找不到解决方案。
用不同的方式测试 pytest.ini , manage.py , wsgi.pysettings.py简单测试 assert 200==200好好工作。
问题出在哪儿?

最佳答案

使用 request.build_absolute_uri() :

 resp = requests.get(request.build_absolute_uri(reverse('admin:index')))

关于python-3.x - 通过 Pytest 和 Requests 测试 Django 时出现 MissingSchema 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48663374/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com