LoginSignup
15
1

mix test.watchの紹介

Last updated at Posted at 2023-11-21

こんにちは!
プログラミング未経験文系出身、Elixirの国に迷い込んだ?!見習いアルケミストのaliceと申します。
今回はmix test.watchについて学んだことをまとめます。

目的

mix test.watchの使いどころ、導入方法の紹介。

実行環境

Windows 11 + WSL2 + Ubuntu 22.04
Elixir v1.14.3
Erlang v26.0.2
Phoenix v1.7.10

mix test.watchとは?

Elixirのライブラリ。mix testをソースコードを編集する度に実行してくれます。
ElixirでTDDを実施するために必須のライブラリとも言えます。
↓これを毎回実行してくれる。

nov21
mix test
..
Finished in 0.00 seconds (0.00s async, 0.00s sync)
1 test, 1 doctest, 0 failures

導入方法

公式ドキュメント

下記から画像の通りコピーでもいけます。

image.png

mix.exsに書き込んで、mix deps.getします。

mix.exs
 defp deps do
    [
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
+     {:mix_test_watch, "~> 1.1"}
    ]
  end
bash
mix deps.get

使ってみた

mix test.watchの実行

bash
mix test.watch
..
Finished in 0.00 seconds (0.00s async, 0.00s sync)
1 test, 1 doctest, 0 failures

まずは変更なしの状態。mix testを手動で実行したときと同じ結果です。

ソースコードを編集してテストを落としてみる

lib/nov21.ex
defmodule Nov21 do
  @moduledoc """
  Documentation for `Nov21`.
  """

  @doc """
  Hello world.

  ## Examples

      iex> Nov21.hello()
      :world

  """
  def hello do
-    :world
+    :hoge
  end
end
bash
Running tests...
Compiling 1 file (.ex)


  1) test greets the world (Nov21Test)
     test/nov21_test.exs:5
     Assertion with == failed
     code:  assert Nov21.hello() == :world
     left:  :hoge
     right: :world
     stacktrace:
       test/nov21_test.exs:6: (test)



  2) doctest Nov21.hello/0 (1) (Nov21Test)
     test/nov21_test.exs:3
     Doctest failed
     doctest:
       iex> Nov21.hello()
       :world
     code:  Nov21.hello() === :world
     left:  :hoge
     right: :world
     stacktrace:
       lib/nov21.ex:11: Nov21 (module)


Finished in 0.01 seconds (0.00s async, 0.01s sync)
1 test, 1 doctest, 2 failures

Randomized with seed 762138

自動でmix testが走り、テストが落ちました!

実際には先にテストを書き、そのテストを満たす最低限の実装をしていくことになります。
つまり、今はレッドの状態ということになります。
(TDDの演習は別記事化します。今回はmix test.watchの紹介までとします)

~Elixirの国のご案内~

↓Elixirって何ぞや?と思ったらこちらもどぞ。Elixirは先端のアレコレをだいたい全部できちゃいます:laughing::sparkles::sparkles:

↓ゼロからElixirを始めるなら「エリクサーチ」がおすすめ!私もエンジニア未経験から学習中です。

We Are The Alchemists, my friends!:bouquet:1
Elixirコミュニティは本当に優しくて温かい人たちばかり!
私が挫折せずにいられるのもこの恵まれた環境のおかげです。
まずは気軽にコミュニティを訪れてみてください。2

  1. @torifukukaiouさんのAwesomeな名言をお借りしました。Elixirコミュニティを一言で表すと、これに尽きます。

  2. @kn339264さんの素敵なスライドをお借りしました。Elixirコミュニティはいろんな形で活動中!

15
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
1