简体   繁体   中英

Pytest-django dependency injection

How does pytest-django know whether to inject a test function with a RequestFactory or Client instance?

def test_with_client(client):
    response = client.get('/')
    assert response.content == 'Foobar'


def test_details(rf):
    request = rf.get('/customer/details')
    response = my_view(request)
    assert response.status_code == 200

In other words: how can you make sure the input fixture is of a certain type?

pytest doesn't inject based on type but on name . The name of the input parameter is matched to registered fixtures.

See the docs here , but in short

import pytest

@pytest.fixture
def connection():
    return Connection()

def test_my_object(connection):
    target = MyObject(connection)

    assert ...

You can use type annotations to help PyCharm etc infer the correct type, but these are not used by pytest.

Short answer: you shouldn't be running these checks for each test. Using test argument names to determine which fixtures are injected is a core component of pytest , and littering each test which uses fixtures with assert isinstance(my_fixture, MyFixtureType) for each fixture is redundant.

pytest-django is already testing that the client and rf fixtures are of the correct type:

def test_client(client):
    assert isinstance(client, Client)
...
def test_rf(rf):
    assert isinstance(rf, RequestFactory)

Edit:

As you've passed the fixture as param to the test method, if you got the fixture name correctly, you don't have to check anything.

Here's an example:

@pytest.fixture(scope='session')                                                                                                                                                                            
def factory():                                                                                                                                                                                              
    return RequestFactory(HTTP_X_REQUESTED_WITH='XMLHttpRequest')                                                                                                                                        

@pytest.fixture(scope='session')                                                                                                                                                                            
def client():                                                                                                                                                                                               
    return Client(HTTP_X_REQUESTED_WITH='XMLHttpRequest')   

Now, in your test method you can take one or both of the fixtures and work with them eg:

def test_foo(client):
    # Do stuff     

def test_bar(factory):
    # Do stuff           

Original answer:

You can check for the type of the input fixture using isinstance .

from django.test import RequestFactory, Client

Inside the test method test for Client :

if isinstance(client, Client):
    # This is a Client instance

Similarly for RequestFactory :

if instance(rf, RequestFactory):
    # This is a RequestFactory instance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM