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 檔案中的所有測驗中都收到此錯誤?客戶端的固定范圍應該是會話,并且應該將 autouse 設定為 true 嗎?
E AttributeError: 'TestUserRouter' object has no attribute 'get'
E AttributeError: 'TestHelloRouter' object has no attribute 'get'
uj5u.com熱心網友回復:
由于這些是類,因此在我看來,您只是缺少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}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/406794.html
標籤:
上一篇:顫動凍結:等于物件的型別不同
