Integration Testing Phoenix Applications
Test your Phoenix controllers, views, and channels to ensure the different parts of your web application work together.
Test Web App Interactions
Welcome to integration testing for Phoenix applications! While unit tests check individual components, integration tests ensure different parts of your web application work together seamlessly.
- They simulate real user interactions.
- They cover routes, controllers, views, and channels.
- They build confidence that your app's main flows function correctly.
Simulating HTTP Requests
Phoenix.ConnTest is your primary tool for testing HTTP requests. It allows you to simulate requests (GET, POST, PUT, DELETE) to your Phoenix application without needing a live server.
We use functions like get/2, post/3, and put/3 to send requests and then assert on the resulting connection (conn).
defmodule MyPhoenixAppWeb.ExampleTest do
use ExUnit.Case, async: true
import Phoenix.ConnTest
# Assume a simple router route: get "/hello", PageController, :hello
# and PageController.hello renders a page with "Hello World"
test "can get a simple page" do
# Simulate a GET request to /hello
conn = get("/hello")
# Assert the HTTP status code
assert conn.status == 200
# Assert the response body contains "Hello World"
assert html_response(conn, 200) =~ "Hello World"
end
endAll lessons in this course
- Unit Testing with ExUnit
- Integration Testing Phoenix Applications
- Mocking, Stubs, and Test Data
- Property-Based Testing with StreamData