ExUnitで特定のテストだけ実行・スキップする方法
特定のテストだけ実行する
ファイル単位で指定して実行する
mix test
に引数を渡すことで、そのファイルだけテストを実行できます。
$ mix test test/my_test.exs
行番号を指定して実行する
ファイル単位の指定に加えて、:5
のように行番号を指定できます。
$ mix test test/my_test.exs:5
タグ単位で指定して実行する
モジュール属性として@tag :hoge
、または@tag hoge: true
をつけ、mix test
に--only
オプションを渡すと、そのタグが付与されたテストだけを実行できます。@moduletag
でも同様のことが可能です。
defmodule FirstTest do use ExUnit.Case, async: true test "..." do ... end @tag :need_database test "..." do ... end end
defmodule SecondTest do use ExUnit.Case, async: true @moduletag :need_database ... end
$ mix test --only need_database
前回failしたテストだけ実行する
v1.3から入る機能ですが、--stale
オプションをつけることで前回落ちたテストだけを再度実行することができます。
間違い。前回テストを実行したとき以降に変更を加えたモジュールが影響するテストだけ実行できます。
$ mix test --stale
特定のテストだけスキップする
モジュール単位でスキップする
@moduletag :skip
、または@moduletag skip: true
を書くと、それ以降のtest
が全てスキップされます。
defmodule MyTest do use ExUnit.Case, async: true @moduletag :skip ... end
個別にスキップする
@tag :skip
、または@tag skip: true
を書くと、その直後のtest
がスキップされます。
defmodule MyTest do use ExUnit.Case, async: true @tag :skip test "hoge" do ... end test "fuga" do ... end end
タグ単位で指定してスキップする
--exclude
オプションを付与することで、特定のタグが付いたテストをスキップすることができます。
$ mix test --exclude need_database
自動で付与されるタグ
ExUnitによって付与されるタグがいくつか存在します。
:case
:file
:line
:test
:async
:type
:registered
:describe
以下のタグは、自分で設定することで便利に使えるものです。個別の詳細についてはまた別の機会に。
:capture_log
:skip
:timeout
:report
これらのタグも--only
などで利用できます。
$ mix test --only case:MyAppTest
余談ですが、実際には--only
は--include
と--exclude
の合わせ技のためのショートカットのようなもので、以下の2つは等価です。test
タグは全てのテストに付与されるので、それを除外した上で、need_database
タグが付いたものをテストする、という意味です。
$ mix test --only need_database
$ mix test --include need_database --exclude test