Learn how to write effective tests for Phoenix contexts and controllers using ExUnit
Learn how to write effective tests for Phoenix contexts and controllers using ExUnit.
mix test
to run all tests, or pass a folder/file (e.g., mix test test/shop/
) to target specific groupsmix test test/shop/promotions_test.exs:13
lib/
structure: test/shop/
for contexts, test/shop_web/
for controllers and web layertest/support/fixtures/
contains helper code and fixtures for test dataConnCase
: sets up the connection and helpers for testing controllersDataCase
: sets up data and helpers for testing contexts/modules that interact with Ectoasync: true
in tests for parallel execution, but only if your DB supports it (Postgres yes, SQLite no)list_products
, create_product
, by calling them directly and asserting on resultsConnCase
, which provides a fake connection (conn
) for requestsget(conn, "/products")
and assert html_response(conn, 200) =~ "expected text"
to_string/1
in assertions against HTML response textlive/2
instead of get/2
Phoenix’s testing suite is flexible and idiomatic, comes with helpers for context (data) and controller (web) tests, supports fixtures and async by default, and encourages structure and clear assertions for robust, maintainable test coverage of your web app’s backend and frontend layers.