Shared Test Factory Package for all Platform Django repos
Project description
iil-testkit
Shared Test Factory Package for all Platform Django repos — ADR-100
Installation
pip install iil-testkit
In requirements-test.txt:
iil-testkit>=0.2.0
With DRF support:
iil-testkit[drf]>=0.2.0
Quick Setup
In your tests/conftest.py:
pytest_plugins = ["iil_testkit.fixtures"]
Factories
# tests/factories.py — import shared, extend repo-specific
from iil_testkit.factories import UserFactory # noqa: F401
import factory
class MyModelFactory(factory.django.DjangoModelFactory):
class Meta:
model = "myapp.MyModel"
user = factory.SubFactory(UserFactory)
For multi-tenant repos:
from iil_testkit.contrib.tenants import TenantFactory # noqa: F401
Fixtures
| Fixture | Description |
|---|---|
db_user |
Standard active user, saved to DB |
staff_user |
User with is_staff=True |
admin_user |
User with is_superuser=True |
api_client |
Unauthenticated Django Client |
auth_client |
Django Client logged in as db_user |
staff_client |
Django Client logged in as staff_user |
drf_api_client |
Unauthenticated DRF APIClient (skips if DRF missing) |
drf_auth_client |
DRF APIClient authenticated as db_user (skips if DRF missing) |
def test_should_view_dashboard(auth_client):
response = auth_client.get("/dashboard/")
assert response.status_code == 200
def test_should_require_auth(api_client):
from iil_testkit.assertions import assert_redirects_to_login
response = api_client.get("/protected/")
assert_redirects_to_login(response)
Assertion Helpers
from iil_testkit.assertions import (
assert_redirects_to_login,
assert_htmx_response,
assert_no_n_plus_one,
assert_form_error,
)
| Helper | Description |
|---|---|
assert_redirects_to_login(response) |
Asserts 301/302 to /login or /accounts/login |
assert_htmx_response(response) |
Asserts no full <html> page in HTMX partial |
assert_no_n_plus_one(queries, threshold=5) |
Asserts query count within threshold |
assert_form_error(response, field, message) |
Asserts form field error in context |
Naming Convention Plugin (ADR-057)
Auto-registered when installed. All test functions must follow test_should_*:
Naming convention violations — 1 test(s) must start with 'test_should_':
tests/test_foo.py::test_login
Opt-out per test:
@pytest.mark.no_naming_convention
def test_legacy_name(): ...
Opt-out globally: pytest --relax-naming
Warn instead of error: iil_naming_mode = "warn" in pyproject.toml
Architecture
See ADR-100.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file iil_testkit-0.3.0.tar.gz.
File metadata
- Download URL: iil_testkit-0.3.0.tar.gz
- Upload date:
- Size: 28.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b1df285a45c400dddadc8965b3389a7ad495295107d1a80ee534178207dec97d
|
|
| MD5 |
942c499647c5acf2fe07ed792cf81bdc
|
|
| BLAKE2b-256 |
3e7af0ebe2c1f56d2fd9ba50eaa396e60a6adfbc806e0f9ae6c48164fbbc4cae
|
File details
Details for the file iil_testkit-0.3.0-py3-none-any.whl.
File metadata
- Download URL: iil_testkit-0.3.0-py3-none-any.whl
- Upload date:
- Size: 16.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f040e286259c1f59b8af5f510ae5e80a20275b8ac690bba67e2bf8e1ac7502e
|
|
| MD5 |
dd8fe3ac905de608c1cccdd759536eb3
|
|
| BLAKE2b-256 |
d69c170400809325dc01fb1bf71184845463bdb3b1267cc2cb3458b377635fd1
|