Write tests that are pytest-native, high-signal, and behavior-focused.
Apply these rules:
auth_headers is accessed), not just that a helper method mutates a field.unittest.TestCase version if a pytest equivalent already exists.@pytest.mark.asyncio if your environment/setup requires it.Example (high-signal header behavior):
def test_auth_headers_set_before_request() -> None:
client = OpenAI(base_url=base_url, api_key=lambda: "test_bearer_token")
# Access/trigger the behavior you actually rely on at request time
client.auth_headers # should force/ensure correct Authorization value
assert client.auth_headers["Authorization"] == "Bearer test_bearer_token"
Example (parametrize endpoints):
@pytest.mark.parametrize("path", ["/images/generations", "/chat/completions"])
def test_model_stripped_from_body(client: Client, path: str) -> None:
req = client._build_request(
FinalRequestOptions.construct(method="post", url=path, json_data={"model": "gpt-image-1-5", "prompt": "sunset"})
)
assert "model" not in req.json
Example (inline snapshot idea):
def test_schema_snapshot() -> None:
schema = to_strict_json_schema(MyModel)
assert schema == {"type": "object", "additionalProperties": False, "properties": {...}, "required": [...]}
# or use an inline snapshot if your codebase supports it
Example (lazy import regression assertion):
import importlib
import openai
m1 = openai.types
m2 = importlib.reload(openai.types)
assert m1 is m2
Enter the URL of a public GitHub repository