【问题标题】:fixture 'httpbin' not found找不到夹具“httpbin”
【发布时间】:2016-06-11 17:13:05
【问题描述】:

我开始阅读 python requests 库。
我做的第一件事是在本地运行 pytest。

然后我收到此错误消息:

  @pytest.fixture
  def httpbin(httpbin):
    fixture 'httpbin' not found
    available fixtures: tmpdir_factory, httpbin_secure, pytestconfig, httpbin, cov, cache, recwarn, monkeypatch, record_xml_property, capfd, capsys, tmpdir
    use 'py.test --fixtures [testpath]' for help on them.

在测试模块的conftest.py文件中,它尝试创建一个fixture:

@pytest.fixture
def httpbin(httpbin):
    return prepare_url(httpbin)

如何使用 httpbin 固定装置创建 httpbin 固定装置? 我错过了什么?

【问题讨论】:

  • 你为什么把这两个方法都称为参数httpbin
  • @LutzHorn:代码来自请求 conftest.py。它试图通过使用夹具httpbin 创建一个名为httpbin 的夹具。这是我困惑的根源。

标签: python python-requests pytest


【解决方案1】:

httpbin 夹具由 pytest-httpbin 包提供。因此,您需要确保在运行测试之前安装了运行测试/构建文档所需的依赖项

这基本上相当于pip install -r requirements.txt

更详细一点,将所有内容安装到 virtualenv 中:

# Create the virtualenv
$ virtualenv-2.7 --no-site-packages requests-env
$ cd requests-env
$ . bin/activate

# Make sure your venv's version of setuptools is up to date
(requests-env) $ pip install -U setuptools

# Clone a copy of the requests module
(requests-env) $ mkdir src
(requests-env) $ cd src/
(requests-env) $ git clone git@github.com:kennethreitz/requests.git

# Install required dependencies
(requests-env) $ cd requests/
(requests-env) $ pip install -r requirements.txt
(requests-env) $ pip install mock

# Run tests
(requests-env) $ py.test

(注意:pip install mock 应该只适用于 Python

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 2016-07-27
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多