为什么我的 conftest 夹具不适用于所有 FastAPI 测试?
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}
为什么 test_router.py 文件中的所有测试都会收到此错误? client的fixture范围是否应该是session,autouse是否应该设置为true?
E AttributeError: 'TestUserRouter' object has no attribute 'get'
E AttributeError: 'TestHelloRouter' object has no attribute 'get'
因为这些是 类,在我看来你只是缺少 self
作为被测函数的第一个参数。
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}
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}
为什么 test_router.py 文件中的所有测试都会收到此错误? client的fixture范围是否应该是session,autouse是否应该设置为true?
E AttributeError: 'TestUserRouter' object has no attribute 'get'
E AttributeError: 'TestHelloRouter' object has no attribute 'get'
因为这些是 类,在我看来你只是缺少 self
作为被测函数的第一个参数。
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}