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

标签 python-3.x python-requests pytest pytest-django

尝试通过 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.inimanage.pywsgi.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/

相关文章:

python-3.x - 如何在多个异常情况下使用 pytest.raises?

python - 以与 python2 和 python3 兼容的方式将字节写入标准输出

python - 如何使用 pip 在 Windows 上安装 PyQt4?

Python 3.5.1 urllib 没有属性请求

google-app-engine - 在 Google App Engine 中使用请求 python 库

python - 如何使用可调用作为 pytest parametrize() 参数?

python-3.x - 使用 POST 将文件上传到 Flask 应用程序时出现 308 重定向

Python 请求模块在获取请求期间不返回整页

python - 如何阅读 Python 请求的响应?

python - 长时间运行的 py.test 在第一次失败时停止