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

dua*_*ty_ 5 django pytest pytest-django

我安装pytest-django了 pip 并制作了一个测试文件,该文件client根据文档使用了夹具,但是运行它会给我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
Run Code Online (Sandbox Code Playgroud)

Mat*_*our 5

你确定你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 .
    
    Run Code Online (Sandbox Code Playgroud)

    示例代码:

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

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

  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.
    
    Run Code Online (Sandbox Code Playgroud)

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

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

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