Missing Test Directory in Phoenix Project

I am working on doing some CI with a GitHub repo and decided to implement some tests into my project. I was reading over documentation and saw it talks about a generated directory for tests. Any idea why I don’t have this directory and what can I do to generate it?

The simple solution to get one is simply to create a folder “test” in your project root. There is no further magic about it.

Phoenix project follow the normal basic project structure as normal elixir projects as described here Phoenix directory structure.
If you want to know more about the basic project structure then this could be a good place to start Introduction to Mix.

Happy testing.

1 Like

I did as you said and created a test directory in the root and created/copied the files normally found such as the support files and test_helper. When I run mix test I run into this error:

** (CompileError) test/flag_ship_web/views/error_view_test.exs:2: module FlagShipWeb.ConnCase is not loaded and could not be found

I thought this might be caused by the environment and attempted MIX_ENV=test mix test but the result was the same.


defmodule FlagShipWeb.ConnCase do
  @moduledoc """
  This module defines the test case to be used by
  tests that require setting up a connection.

  Such tests rely on `Phoenix.ConnTest` and also
  import other functionality to make it easier
  to build common data structures and query the data layer.

  Finally, if the test case interacts with the database,
  we enable the SQL sandbox, so changes done to the database
  are reverted at the end of every test. If you are using
  PostgreSQL, you can even run database tests asynchronously
  by setting `use FlagShipWeb.ConnCase, async: true`, although
  this option is not recommended for other databases.

  use ExUnit.CaseTemplate

  using do
    quote do
      # Import conveniences for testing with connections
      import Plug.Conn
      import Phoenix.ConnTest
      import FlagShipWeb.ConnCase

      alias FlagShipWeb.Router.Helpers, as: Routes

      # The default endpoint for testing
      @endpoint FlagShipWeb.Endpoint

  setup tags do
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(FlagShip.Repo)

    unless tags[:async] do
      Ecto.Adapters.SQL.Sandbox.mode(FlagShip.Repo, {:shared, self()})

    {:ok, conn: Phoenix.ConnTest.build_conn()}

The test/support directory isn’t compiled by default. Usually you’ll want something like the following in your mix.exs:

def project do
  elixirc_paths: elixirc_paths(Mix.env),

defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_),     do: ["lib"]
1 Like

I recommend you make a brand new project on the side and just compare its default testing boilerplate with what you already have.

Make double sure you have the latest phx_new generator though!

$ mix archive # this should output something like the below:
* hex-0.20.5
* phx_new-1.5.3

If phx_new is an older version then do this:

$ mix archive uninstall phoenix-YOUR.VERSION.HERE
$ mix archive.install hex phx_new 1.5.3

Then generate a new project somewhere and just compare side by side until you make it work. It’s a little laborious but it will still be much faster than bumping from one error to another for hours.