Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my conftest fixture is not applying to all FastAPI tests?

conftest.py

import pytest
from ...main import app as starter
from fastapi.testclient import TestClient


@pytest.fixture(autouse=True, scope="module")
def client():
    client = TestClient(starter)
    return client

test_main.py

import pytest


@pytest.mark.unit
def test_root(client):
    response = client.get("/")
    assert response.json() == {"message": "Hello Bigger Applications!"}

test_router.py

import pytest


@pytest.mark.unit
class TestHelloRouter:
    def test_hello(client):
        response = client.get("/hello")
        assert response.json() == {"message": "Hello Router!"}


@pytest.mark.unit
class TestUserRouter:
    def test_read_users(client):
        response = client.get("/users/")
        assert response.json() == [{"username": "Rick"}, {"username": "Morty"}]

    def test_read_user_me(client):
        response = client.get("/users/me")
        assert response.json() == {"username": "fakecurrentuser"}

    def test_read_user(client):
        username = "unit-test"
        response = client.get(f"/users/{username}")
        assert response.json() == {"username": username}

Why am I receiving this error for all the tests in the test_router.py file? Should the fixture scope of client be session, and should autouse be set to true?

E       AttributeError: 'TestUserRouter' object has no attribute 'get'
E       AttributeError: 'TestHelloRouter' object has no attribute 'get'

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

As these are classes, it looks like to me that you are just missing self as the first argument to your functions under test.

mport pytest


@pytest.mark.unit
class TestHelloRouter:
    def test_hello(self, client):
        response = client.get("/hello")
        assert response.json() == {"message": "Hello Router!"}


@pytest.mark.unit
class TestUserRouter:
    def test_read_users(self, client):
        response = client.get("/users/")
        assert response.json() == [{"username": "Rick"}, {"username": "Morty"}]

    def test_read_user_me(self, client):
        response = client.get("/users/me")
        assert response.json() == {"username": "fakecurrentuser"}

    def test_read_user(self, client):
        username = "unit-test"
        response = client.get(f"/users/{username}")
        assert response.json() == {"username": username}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading