Tests should validate specific, well-defined behaviors with clear assertions that directly relate to what's being tested. Avoid writing overly general or unfocused tests that try to cover too much at once, as these are often harder to maintain and debug when they fail.
Tests should validate specific, well-defined behaviors with clear assertions that directly relate to what’s being tested. Avoid writing overly general or unfocused tests that try to cover too much at once, as these are often harder to maintain and debug when they fail.
Good tests:
For example, instead of writing a general test:
# Too general and unfocused
def test_model_attributes():
model = Model()
# Testing too many things at once
assert model.field == "default"
assert model._private_attr is not None
assert callable(model.method)
Write specific, targeted tests:
def test_private_attribute_not_skipped_during_ns_inspection() -> None:
# Clear comment explaining the test's purpose
# It is important for the enum name to start with the class name
# (it previously caused issues with qualname comparisons):
class Fullname(str, Enum):
pass
class Full(BaseModel):
_priv: object = Fullname
# Specific assertion testing exactly one behavior
assert isinstance(Full._priv, ModelPrivateAttr)
When parameterizing tests, ensure each parameter set tests a distinct case that belongs together. If test cases require different assertion logic or warning checks, they should be in separate test functions.
Enter the URL of a public GitHub repository