Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

5.0 KB
Raw Blame

Python tests

  • Read guidance for the production code. Use existing suites (tests/app/, tests/backend/, or the owning root suite) and matching conftest.py/fixtures/ fixtures. UI copy and docs checks belong to their packages.
  • Tests that read files outside tests/, scripts/, or the Python package tree: add those paths to .github/workflows/python-tests.yml change filters, so fixture-only changes trigger pytest.
  • Name tests by outcome. Cover meaningful edge and failure cases. Survive behavior-preserving refactors. Prefer real temporary SQLite/filesystem/tensor operations. Mock external services, expensive models, unavailable hardware; never mock the transaction, authorization, concurrency, or numerical behavior under test.
  • Isolate paths, databases, accounts, clocks, seeds, environment, network behavior. Clean up subprocesses, listeners, patches, resources. Never use production model/database/output directories.
  • Cover affected rollback, revisions, cross-user isolation, retries, cancellation, bounds, cleanup. IndexedDB, Web Locks, focus, rendering belong in webv2 browser tests.
  • Use root pytest commands. CI runs one xdist worker per vCPU; windows-ffmpeg runs two. --dist loadfile in addopts keeps each file on one worker; file assignment and order are arbitrary. Each file sets up its own process-global state (e.g. JWT secret) and passes alone. Verify new files on their own.
  • Full CI: uv run --no-sync pytest -n logical, which fits CI runners that own the machine. Dev box: cap workers (-n 4, or plain pytest). Set OMP_NUM_THREADS when a run must stay light.
  • For maximum local parallelism, use -n <cpus> --dist load (replace <cpus> with the desired worker count, e.g. -n 8). This overrides the configured loadfile mode and can distribute tests from one file across workers; module-scoped fixtures may be set up once per worker.
  • uv run --no-sync mypy scripts/invokeai-web.py uses current exclusions. Optional diagnostic only: not a CI gate, not full type coverage.
  • Default tests assert behavior, not throughput or latency under runner contention. Generous hang ceilings fine. CPU time tolerates contention better than wall time. Hardware or quiet-machine measurements: mark slow (excluded by default and in CI). Dev machines: -m slow for that lane, -m "" for everything.
  • Keep a test's run time to the work it checks: idle seconds add up, and windows-ffmpeg runs the ffmpeg tests on two workers. Check new files with --durations=10 (splits setup, call, teardown).
    • No real waits. Poll intervals, retry backoffs, and grace periods are module-level constants a fixture can shorten. Root conftest.py already shortens the download, installer, and image-index queue polls, so a service's teardown stop() returns at once. Shorten a retry backoff only in tests that script its failures (_fake_move in test_model_install.py): the backoff lets a real Windows file handle close.
    • Passwords hash at bcrypt cost 4 in tests. Request production_password_hashing to assert or measure the production cost.
    • A fresh interpreter costs about 10 s of imports. Spawn one only for isolation (import order, sys.modules, process-global state). Check many cases in one subprocess unless each needs its own cold start.
    • A full gc.collect() walks the worker's whole heap: a third of a second late in a session. Tests that collect repeatedly freeze the pre-existing heap first (test_model_cache_ram_budget.py).
  • Mark platform_independent only when no operating system can change the outcome for the same dependency set: import-graph boundaries in a fresh interpreter, static source scans, deterministic counts or shapes. Windows CI installs the linux dependency set and deselects them; macOS runs a different torch and keeps them. Leave unmarked: numerics (BLAS kernels differ per platform), process spawning (start methods, helper executables), filesystem paths and locking, dependency smoke tests.
  • Mark ffmpeg when a test runs an ffmpeg binary or loads libav, directly or through fixtures and helpers, including bad-input rejection tests. Not when ffmpeg is mocked, or spawned only incidentally on input every decoder rejects. windows-cpu runs Windows Server Core, whose missing video DLLs stop every standard ffmpeg build from loading, so it deselects them; windows-ffmpeg runs only them on a hosted runner. Whole file: module-level pytestmark. Find unmarked tests by reproducing that runner: INVOKEAI_TEST_WITHOUT_FFMPEG=1 uv run --no-sync pytest -m "not slow and not ffmpeg" (root conftest.py hides ffmpeg).
  • State hardware prerequisites and skipped/unavailable devices. CPU or mocked passes do not prove CUDA/ROCm/MPS/XPU behavior.
  • No CPU bf16 matmul unless low precision is the behavior under test. Why: torch's CPU bf16 GEMM faults with an illegal instruction (0xc000001d) on part of the hosted Windows fleet, aborting the worker and failing the job. Run the forward at float32 and assert the width separately (nvfp4 loader tests). Skip on win32 only when bf16 itself is the subject.