name: exunit-testing description: Adding or fixing ExUnit tests (unit, ConnCase, DataCase, LiveView). Use when writing tests, fixing failing tests, or improving test structure and coverage. user-invocable: false
ExUnit Testing
When to Use
- Adding tests for contexts, controllers, LiveView, or other modules (unit or integration).
- Fixing failing tests (determine whether the failure is in implementation or test; fix minimally).
- Improving test structure (describe, setup, tags) or readability.
Principles
- Layout: Mirror source layout:
test/my_app/user_test.exsforlib/my_app/user.ex;test/my_app_web/live/dashboard_live_test.exsfor LiveView. Usedescribeandtest "description"with clear descriptions. - ConnCase: For HTTP; use
get,post, etc. and assert on response and assigns. Set up auth/session in setup when needed. - DataCase: For repo and context tests; use Repo and context functions. Use setup for fixtures; prefer factories or explicit inserts.
- LiveView tests: Use Phoenix.LiveViewTest (render, follow_redirect, element, etc.). Test mount, handle_params, and key handle_events; assert on rendered content and navigation.
- Assertions: Prefer
assert,assert_raise,refute. Use pattern matching in assertions when the shape is important (assert {:ok, x} = ...). Keep expected values explicit. - Setup: Use
setuporsetup_allfor shared context; use@tagfor expensive or integration tests; run withmix test --only tagwhen needed. - Verification: Run
mix test(optionally with path or filter) to verify. Do not change behaviour beyond what is needed to fix the failure.