빠른 참조

ExUnit 치트시트 (Elixir 내장 테스트 프레임워크)

Elixir의 기본 내장 테스트 프레임워크 ExUnit 핵심 치트시트. 테스트 케이스 작성, 단언문(assert/refute), 비동기 테스트, IO 캡처 및 셋업 콜백을 정리합니다.

· 게시일

ExUnit은 Elixir에 기본 내장된 단위 테스트 프레임워크로, 테스트 케이스, 단언문 및 셋업 콜백을 직관적으로 작성할 수 있는 강력한 매크로를 제공합니다.

테스트 케이스 정의 (Test Cases)

defmodule MyTest do
  use ExUnit.Case
  use ExUnit.Case, async: true  # for async

  test "the truth" do
    assert 1 + 1 == 2
  end
end

표준 입출력 및 로그 캡처 (Capture IO & Logs)

import ExUnit.CaptureIO

test "capture io" do
  result = capture_io(fn ->
    IO.puts "sup"
  end)

  assert result == "sup\n"
end
config :ex_unit, capture_logs: true

단언문 및 예외 검증 (Assertions & Refutations)

assert x == y
refute x == y

assert_raise ArithmeticError, fn ->
  1 + "test"
end

assert_raise ArithmeticError, "message", fn -> ... end
assert_raise ArithmeticError, ~r/message/, fn -> ... end

flunk "This should have been an error"

셋업 콜백 및 컨텍스트 매칭 (Setup Callbacks)

setup do
  {:ok, name: "John"}
end

test "it works", %{name: name} do
  assert name == "John"
end
defp my_hook(_context) do
  {:ok, name: "John", age: 54}
end

describe "a block" do
  setup [:my_hook]

  test "John's age", context do
    assert context[:name] == "John"
    assert context[:age] == 54
  end
end

관련 치트시트

관련 주제