robkuz
robkuz

Reputation: 9904

How can I make Mix run only specific tests from my suite of tests?

How can I make Mix run only specific tests from my suite of tests?

When running mix test all tests are executed

Upvotes: 75

Views: 23859

Answers (3)

Ivan Uemlianin
Ivan Uemlianin

Reputation: 1027

A way to run a subset of tests:

  1. use ExUnit.Case.describe/2 to label the set of tests in the _test.ex[s] file, eg:
  describe "String.capitalize/1" do
    ... [some tests]
  end
  1. run mix test with --only describe:"...", eg:
mix test --only describe:"String.capitalize/1"

mix will run the tests, showing how many were excluded, eg:

... [results]
16 tests, 2 failures, 8 excluded

Upvotes: 4

Jose V
Jose V

Reputation: 1854

If you don't want mix test to run certain test files, just rename them. mix test matches on any file ending with _test.ex or _test.exs so all you have to do is rename those files that you don't want to run to something else that does not match, like _test_off.ex.

controller_test.exs -> controller_test_off.exs

Upvotes: 0

robkuz
robkuz

Reputation: 9904

There are 5 ways to run only specific tests with Elixir

  1. run a single file with mix test path_to_your_tests/your_test_file.exs
    This will run all test defined in your_test_file.exs

  2. run a specific test from a specific test file by adding a colon and the line number of that test
    for example mix test path_to_your_tests/your_test_file.exs:12 will run the test at line 12 of your_test_file.exs

  3. define a tag to exclude on your test methods

    defmodule MyTests do
        @tag disabled: true
        test "some test" do
            #testtesttest
        end
    end
    

    on the command line execute your tests like this
    mix test --exclude disabled

  4. define a tag to include on your test methods

    defmodule MyTests do
        @tag mustexec: true
        test "some test" do
            #testtesttest
        end
    end
    

    on the command line execute your tests like this
    mix test --only mustexec

  5. Generally exclude some tagged tests by adding this to your test/test_helper.exsfile
    ExUnit.configure exclude: [disabled: true]

Warning: Mix has an --include directive. This directive is NOT the same as the --only directive. Include is used to break the general configuration (exclusion) from the test/test_helper.exsfile described under 4).

For some reason googling for elixir mix include testsor the like never shows up on my search results therefore I have written this entry and its answer. For more info, see the Mix documentation.

Upvotes: 124

Related Questions