Skip to content

Instantly share code, notes, and snippets.

@adolfont
Created April 27, 2022 16:42
Show Gist options
  • Select an option

  • Save adolfont/3e39a14fb5a61d6733d3b7ab1d76a3ec to your computer and use it in GitHub Desktop.

Select an option

Save adolfont/3e39a14fb5a61d6733d3b7ab1d76a3ec to your computer and use it in GitHub Desktop.
TDD with Livebook v2

TDD in Livebook with ExUnit

Code

defmodule Person do
  defstruct [:first_name, :last_name]

  def new(first_name, last_name) do
    %__MODULE__{first_name: first_name, last_name: last_name}
  end

  def first_name(%__MODULE__{first_name: first_name}), do: first_name

  def last_name(%__MODULE__{last_name: last_name}), do: last_name
end

Tests

ExUnit.start(auto_run: false)

defmodule PersonTest do
  use ExUnit.Case, async: false

  setup_all do
    {:ok, person: Person.new("Adolfo", "Neto")}
  end

  test "person's first name is Adolfo", state do
    assert Person.first_name(state[:person]) == "Adolfo"
  end

  test "person's last name is neto", state do
    assert Person.last_name(state[:person]) == "Neto"
  end
end

ExUnit.run()

Run Tests

To run the tests, click "Reevaluate" in the Elixir cell below "Code" and below "Tests".

Thanks

Thanks to David Bernheisel who pointed me to this blog post by Brooklyn Meyers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment