如何检查是否安装了 pytest-django 以及为什么客户端装置不工作?

How can I check if pytest-django is installed and why doesn't client fixture work?

我用 pip 安装了 pytest-django 并制作了一个测试文件,它根据 docs 使用 client fixture,但是 运行 这给了我 fixture 'client' not found.这是我的 test_homepage.py:

import pytest


@pytest.mark.django_db
def test_homepage(client):
    response = client.get('/')
    assert response.status_code == 200

您确定已安装 pytest-django 吗?我在我的机器上安装了 pytest-django 和 运行 一个简单的项目。

  1. 安装pip install pytest-django
  2. 设置和运行我的示例测试:

    platform linux -- Python 3.4.3 -- py-1.4.30 -- pytest-2.7.2
    rootdir: /home/matt/projects/test_app/src, inifile: pytest.ini
    plugins: django
    collected 1 items 
    
    tests/test_example.py .
    

    示例代码:

    import pytest
    
    @pytest.mark.django_db
    def test_something(client):
            response = client.get('/')
            assert  response.status_code == 200
    

    注意列出的插件和代码的工作。

  3. 卸载为了测试我要删除pip uninstall pytest-django

  4. 再次运行测试py.test

    platform linux -- Python 3.4.3 -- py-1.4.30 -- pytest-2.7.2
    rootdir: /home/matt/projects/test_app/src, inifile: pytest.ini
    collected 1 items 
    
    tests/test_example.py E
    
    ==================================== ERRORS ====================================
    _______________________ ERROR at setup of test_something _______________________
    file /home/matt/projects/test_app/src/tests/test_example.py, line 3
    @pytest.mark.django_db
    def test_something(client):
        fixture 'client' not found
        available fixtures: tmpdir, pytestconfig, recwarn, capsys, capfd, monkeypatch
        use 'py.test --fixtures [testpath]' for help on them.
    

    现在我们可以看到您在项目中指出的相同错误。

所以,如果我是你,我会从你的环境中完全删除 pytestpytest-django,然后重新安装。看起来 pytest-django 尚未安装或由于某种原因未检测到插件。

如果这对解决项目没有帮助,您的另一个选择是 运行 py.test --traceconfig 这将为您提供更详细的 运行 过程输出。执行此操作然后将输出添加到您的问题中。