Testing
The pytest plugin (loaded automatically) and ninja_devx.testing.clients.
See the Testing guide.
Fixtures
| Fixture |
Returns |
Description |
ninja_client |
Callable[..., AuthenticatedClient] |
ninja_client(ControllerOrRouter, user=None, container=None, scope=None, **options). |
ninja_async_client |
Callable[..., AuthenticatedAsyncClient] |
ninja_async_client(ControllerOrRouter, user=None, **options): an async test client. |
ninja_contract |
Callable[..., object] |
ninja_contract(api): a schemathesis schema for schemathesis.pytest.from_fixture. |
captured_commits |
Callable[..., AbstractContextManager[list[Callable[[], object]]]] |
with captured_commits() as callbacks: ... runs on-commit work at the end. |
openapi_snapshot |
Callable[..., None] |
openapi_snapshot(api, name="openapi") compares the schema with a stored snapshot. |
strict_queries |
Generator[None] |
Raise zeal.NPlusOneError for any N+1 in this test, regardless of ZEAL_RAISE. |
Options
| Option |
Where |
Default |
Description |
--update-snapshots |
command line |
False |
ninja-devx: rewrite OpenAPI snapshots instead of comparing them |
ninja_devx_warn_async_db |
ini |
True |
ninja-devx: warn when async tests use the database without transaction=True |
client_for()
def client_for(controller: type[Controller], *, user: object | None = None, container: ContainerLike | None = None, scope: Scope | None = None, **options: Unpack[ControllerOptions]) -> AuthenticatedClient: ...
A test client for controller.as_router(...); paths are relative to the router.
| Parameter |
Type |
Default |
Description |
controller |
type[Controller] |
— |
The controller class. |
user |
object \| None |
None |
Sent as request.user with every request (override per request with user=). |
container |
ContainerLike \| None |
None |
Container for as_router(). |
scope |
Scope \| None |
None |
Scope for as_router(). |
**options |
Unpack[ControllerOptions] |
— |
ControllerOptions for as_router(). |
async_client_for()
def async_client_for(controller: type[Controller], *, user: object | None = None, container: ContainerLike | None = None, scope: Scope | None = None, **options: Unpack[ControllerOptions]) -> AuthenticatedAsyncClient: ...
| Parameter |
Type |
Default |
Description |
controller |
type[Controller] |
— |
The controller class. |
user |
object \| None |
None |
Sent as request.user with every request. |
container |
ContainerLike \| None |
None |
Container for as_router(). |
scope |
Scope \| None |
None |
Scope for as_router(). |
**options |
Unpack[ControllerOptions] |
— |
ControllerOptions for as_router(). |
assert_max_queries()
def assert_max_queries(limit: int, *, using: str = DEFAULT_DB_ALIAS) -> Generator[CaptureQueriesContext]: ...
Fail when the block runs more than limit queries (catches N+1 regressions).
| Parameter |
Type |
Default |
Description |
limit |
int |
— |
Maximum number of queries in the block. |
using |
str |
DEFAULT_DB_ALIAS |
Database alias to watch. |
assert_max_hops()
def assert_max_hops(limit: int) -> Generator[HopCounter]: ...
Fail when the block switches from the event loop to a thread more than limit times.
| Parameter |
Type |
Default |
Description |
limit |
int |
— |
Maximum sync_to_async thread hops in the block. |
capture_commits()
def capture_commits(*, using: str = DEFAULT_DB_ALIAS, execute: bool = True) -> Generator[list[Callable[[], object]]]: ...
Collect transaction.on_commit callbacks (after_commit, OnCommitTaskQueue).
| Parameter |
Type |
Default |
Description |
using |
str |
DEFAULT_DB_ALIAS |
Database alias. |
execute |
bool |
True |
Run the callbacks when the block exits. |
contract_schema()
def contract_schema(api: NinjaAPI, *, path_prefix: str | None = None) -> OpenApiSchema: ...
A schemathesis schema for api, calling the Django WSGI app in-process.
| Parameter |
Type |
Default |
Description |
api |
NinjaAPI |
— |
The NinjaAPI to test. |
path_prefix |
str \| None |
None |
API root path (default: where it is mounted in the URLconf). |
make_context()
def make_context(user: UserT, tenant: TenantT, **metadata: str) -> RequestContext[UserT, TenantT]: ...
A RequestContext for tests: make_context(user, None).
| Parameter |
Type |
Default |
Description |
user |
UserT |
— |
The acting user. |
tenant |
TenantT |
— |
The tenant, or None. |
**metadata |
str |
— |
String metadata. |
sample()
def sample(schema: type[SchemaT]) -> dict[str, object]: ...
A payload for schema built from defaults, examples and field types.
| Parameter |
Type |
Default |
Description |
schema |
type[SchemaT] |
— |
The pydantic schema (usually the request body). |
samples()
def samples(schema: type[SchemaT], count: int) -> list[dict[str, object]]: ...
count payloads for schema with distinguishable scalar values.
| Parameter |
Type |
Default |
Description |
schema |
type[SchemaT] |
— |
The pydantic schema (usually the request body). |
count |
int |
— |
How many payloads to build. |