zoedsoupe

zoedsoupe

That question maybe look as too generic but I really want to have an initial idea about how do you make integration/acceptance tests on a Phoenix API.

Some point are:

  • do you use any libraries or builtins function in ConnCase/ChannelCase are enough?
  • any suggestions with extra content and material about this topic and phoenix/elixir?
  • could you provide a simple example test that can’t be covered with Phoenix’s builtin functions

Showing Posts 1 to 4

ityonemo

ityonemo

An API? As in not serving html? Usually I implement these as full integration tests. I use an external library (e.g. Finch) and construct the http request from scratch instead of using a Conn. To do async I either stuff $callers into the user-agent header and use that to connect the cowboy process back to the test, or I use Bypass and instantiate a webserver with the API router as a plug for bypass’ Conn. I almost never use conncase or channelcase in these instances.

stefanchrobot

stefanchrobot

I write the integration tests in ExUnit which has the benefit of the tests being ran as part of the normal test suite. I abstract away any Phoenix helpers since they are taking some shortcuts. I follow the approach of Hex: we should be testing behaviours, not the implementation, but the unit of behaviour here is an API endpoint - it’s the public contract. I try to avoid shared setups as much as possible - this way the tests are self-contained. I invest into building helpers to combat code duplication. UserUI is a module with helpers that mimic user actions in the web app (creating account, sending a message, etc.). MobileApi is a module with helpers that basically do HTTP requests but are tailored to this specific API (API-key based auth with JSON). Example test with test helpers:

defmodule MobileApiTest do
  use MyApp.Case, async: true

  alias MyApp.TestHelpers.UserUI
  alias MyApp.TestHelpers.MobileApi

  describe "GET /api/mobile/messages" do
    test "lists messages sent to the person" do
      # Setup...
      account = UserUI.create_account!()
      message1 = UserUI.create_message!(account)
      # More setup...

      assert MobileApi.get("/api/mobile/messages", api_key) == %{
               status: 200,
               body: %{
                 "messages" => [
                   %{
                     "id" => message2.id,
                     "subject" => message2.subject,
                     "sent_at" => DateTime.to_iso8601(message2.inserted_at)
                   },
                   %{
                     "id" => message1.id,
                     "subject" => message1.subject,
                     "sent_at" => DateTime.to_iso8601(message1.inserted_at)
                   }
                 ]
               }
             }
    end

    test "returns 401 on expired API key" do
      # Setup...

      assert %{
               status: 401,
               body: %{"error" => "expired_api_key"}
             } = MobileApi.get("/api/mobile/messages", api_key)
    end
  end
end

defmodule MyApp.TestHelpers.MobileApi do
  @moduledoc """
  Test helpers for interacting with the mobile app API.
  """

  alias Plug.Conn
  alias Phoenix.ConnTest
  require Phoenix.ConnTest

  # The default endpoint for testing.
  @endpoint MyApp.Endpoint

  def get(url, api_key \\ nil) do
    %{status: status, resp_body: body} =
      api_key
      |> build_conn()
      |> ConnTest.get(url)

    %{status: status, body: if(body != "", do: Jason.decode!(body), else: "")}
  end

  defp build_conn(api_key) do
    conn =
      ConnTest.build_conn()
      |> Conn.put_req_header("accept", "application/json")

    if api_key do
      conn |> Conn.put_req_header("authorization", "Bearer #{api_key}")
    else
      conn
    end
  end
end
LostKobrakai

LostKobrakai

While its certianly possible to do integration tests in elixir itself I also want to mention that you can use other tools as well. I’ve talked to people using e.g. js based browser testing tools to test liveview apps. So if you have experience in non elixir tools already you might not need to shift gears at all.

karlosmid

karlosmid

Hi!

Using code coverage: mix test --cover be sure to have:

  1. source code statement coverage
  2. combinations for branch (if then else) coverage
  3. do not forget test data
— All posts loaded —

Where Next? Top

Trending in Discussions Top

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews