Run a Single Elixir Test

Here are a couple ways to run only one elixir test at a time.

Start New Mix Project

The simplest way to get an Elixir project set up for testing is with mix, which comes with Elixir distributions.

mix new my_project

Run All Tests

To run all tests, by default, you run:

mix test

This will execute all *_test.exs files in the test/ directory.

Run Single File

To run a single test suite, you specify it by its file name:

mix test test/my_project_test.exs

Run Single Block

To run a single describe or test block, target it by file name + line number:

mix test test/my_project_test.exs:11

In this example, if line 11 is a single test, it'll run just that test. If it's a describe, it'll run all tests within the describe.

Run Tagged Groups

To run a grouping of tests, you can use tags. This requires changing the test file contents, adding a tag decorator. The tag can have an arbitrary name and value.

Maybe it's named for a feature (eg @tag feature: "auth"), a speed of tests (eg, @tag :slow), a class of test (eg, @tag type: "integ") or stage of your testing process (eg, @tag :only). For instance:

@tag :slow
test "a big operation" do
  # test assertions that are slow
end

To run this example, you'd type:

mix test --only slow

You can also stack tags. To recreate the class test.only of other test frameworks, you could adjust the example:

@tag :only
@tag :slow
test "a big operation" do
  # test assertions that are slow
end
mix test --only only

Preclude Certain Tests Automatically

Instead of having to pass in commandline options to mix test, you can also automatically set up which tags to run. The most common and obvious reason to do this would be for skipping tests that were marked "pending" or "todo".

Before any tests are run, ExUnit looks for a file called test_helper.exs. This is run before any of your tests are run. So, it's a great place for any setup code.

To configure ExUnit to skip pending tests (ie, @tag :pending), create a test/test_helper.exs that reads:

ExUnit.start()
ExUnit.configure(exclude: :pending)

Restart your mix test process, and you'll be able to auto-skip pending tests.