你想要的测试可以很容易地使用pytest进行:
import pytest
def test_phonenumber():
pn = PhoneNumber(id=1, country="country", country_code="code", number="number", extension="extension")
assert pn.id == 1
assert pn.country == 'country'
assert pn.country_code == 'code'
assert pn.number == 'number'
assert pn.extension == 'extension'
但我同意这个评论:
总的来说,你不应该像这样编写测试。 Pydantic已经有很好的测试套件(包括像您提议的单元测试)。你的测试应该覆盖你编写的代码和逻辑,而不是导入的包。
如果你有一个像PhoneNumber
这样没有任何特殊/复杂验证的模型,那么写一个仅实例化它并检查属性的测试就不会那么有用。类似于这样的测试就像在测试 Pydantic 本身。
但是,如果你的模型有一些特殊/复杂的验证函数,例如它检查country
和country_code
是否匹配:
from pydantic import BaseModel, root_validator
class PhoneNumber(BaseModel):
...
@root_validator(pre=True)
def check_country(cls, values):
"""Check that country_code is the 1st 2 letters of country"""
country: str = values.get('country')
country_code: str = values.get('country_code')
if not country.lower().startswith(country_code.lower()):
raise ValueError('country_code and country do not match')
return values
那么针对该特定行为的单元测试将更加有用:
import pytest
def test_phonenumber_country_code():
"""Expect test to fail because country_code and country do not match"""
with pytest.raises(ValueError):
PhoneNumber(id=1, country='JAPAN', country_code='XY', number='123', extension='456')
另外,正如我在评论中提到的,如果您提到了FastAPI,并且您将此模型用作路由定义的一部分(无论是请求参数还是响应模型),那么更有用的测试将是确保您的路由能够正确使用您的模型。
@app.post("/phonenumber")
async def add_phonenumber(phonenumber: PhoneNumber):
"""The model is used here as part of the Request Body"""
# Do something with phonenumber
return JSONResponse({'message': 'OK'}, status_code=200)
from fastapi.testclient import TestClient
client = TestClient(app)
def test_add_phonenumber_ok():
"""Valid PhoneNumber, should be 200/OK"""
# This would be what the JSON body of the request would look like
body = {
"id": 1,
"country": "Japan",
"country_code": "JA",
"number": "123",
"extension": "81",
}
response = client.post("/phonenumber", json=body)
assert response.status_code == 200
def test_add_phonenumber_error():
"""Invalid PhoneNumber, should be a validation error"""
# This would be what the JSON body of the request would look like
body = {
"id": 1,
"country": "Japan",
# `country_code` is missing
"number": 99999, # `number` is int, not str
"extension": "81",
}
response = client.post("/phonenumber", json=body)
assert response.status_code == 422
assert response.json() == {
'detail': [{
'loc': ['body', 'country_code'],
'msg': 'field required',
'type': 'value_error.missing'
}]
}