مرجع سريع

ورقة غش إطار عمل الاختبار ExUnit في Elixir

دليل مرجعي سريع لإطار الاختبار المدمج ExUnit في Elixir: حالات الاختبار، التوكيدات، رفض الشروط، التقاط الإدخال/الإخراج، واستدعاءات الإعداد.

· تاريخ النشر

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

أوراق غش ذات صلة

موضوعات ذات صلة