Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I installed pytest-django with pip and made a test file, which uses the client fixture as per the docs, but running this gives me fixture 'client' not found. Here's my test_homepage.py:

import pytest


@pytest.mark.django_db
def test_homepage(client):
    response = client.get('/')
    assert response.status_code == 200
like image 780
duality_ Avatar asked Jun 26 '15 12:06

duality_


1 Answers

Are you sure you have pytest-django installed. I installed pytest-django on my machine and ran a simple project.

  1. Install pip install pytest-django
  2. Setup and run my sample 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
    plugins: django
    collected 1 items 
    
    tests/test_example.py .
    

    Sample code:

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

    Note the plugins listed and the code works.

  3. Uninstall for the sake of testing I am going to remove pip uninstall pytest-django

  4. Once again run the tests 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.
    

    Now we can see the same error you are stating in your project.

So, if I were you I would completely remove pytest and pytest-django from your environment then re-install. It looks like either pytest-django has not been installed or that the plugins are not being detected for some reason.

If this does not help solve the project your other option is to run py.test --traceconfig this will give you a more verbose output of the run process. Do this then add the output to your question.

like image 97
Matt Seymour Avatar answered Sep 28 '22 11:09

Matt Seymour