Generating multiple test cases from a single test in Elixir using a for-comprehension!

amos-kibet

amos-kibet

Created 3 months ago

🧪 TIL: You can generate multiple test cases from a single test in Elixir using a for-comprehension! Super clean way to test similar scenarios.

for status <- [:active, :archived, :close, :delete, :draft, :planned, :public] do
  @tag status: status
  test "filters courses by status: #{status}", %{status: status} do
    %MyApp.Course{id: course_id} = course_fixture(%{status: status})

    assert {:ok, [course_from_db]} = MyApp.Course.list_courses(status)

    assert course_from_db.id == course_id
  end
end

7 tests, 1 block of code! 🎯

Credits to Jeff Sandberg. Read more here.