Prove (Prove v0.1.1) View Source

Prove provides the macros prove and batch to write simple tests shorter.

A prove is just helpful for elementary tests. Prove generates one test with one assert for every prove.

Example

defmodule NumTest do
  use ExUnit.Case

  import Prove

  defmodule Num do
    def check(0), do: :zero

    def check(x) when is_integer(x) do
      case rem(x, 2) do
        0 -> :even
        1 -> :odd
      end
    end

    def check(_), do: :error
  end

  describe "check/1" do
    prove Num.check(0) == :zero

    batch "returns :odd or :even" do
      prove Num.check(1) == :odd
      prove Num.check(2) == :even
      prove "for big num", Num.check(2_000) == :even
    end

    batch "returns :error" do
      prove Num.check("1") == :error
      prove Num.check(nil) == :error
    end
  end
end

The example above generates the following tests:

$> mix test --trace test/num_test.exs --seed 0

NumTest [test/num_test.exs]
  * test check/1 Num.check(0) == :zero (0.00ms) [L#20]
  * test check/1 returns :odd or :even Num.check(1) == :odd (0.00ms) [L#22]
  * test check/1 returns :odd or :even Num.check(2) == :even (0.00ms) [L#22]
  * test check/1 returns :odd or :even for big num Num.check(2000) == :even (0.00ms) [L#22]
  * test check/1 returns :error Num.check("1") == :error (0.00ms) [L#28]
  * test check/1 returns :error Num.check(nil) == :error (0.00ms) [L#28]


Finished in 0.05 seconds (0.00s async, 0.05s sync)
6 tests, 0 failures

Randomized with seed 0

The benefit of prove is that tests with multiple asserts can be avoided. The example above with regular tests:

...
  describe "check/1" do
    test "returns :zero" do
      assert Num.check(0) == :zero
    end

    test "returns :odd od :even" do
      assert Num.check(1) == :odd
      assert Num.check(2) == :even
      assert "for big num", Num.check(2_000) == :even
    end

    test "returns :error" do
      assert Num.check("1") == :error
      assert Num.check(nil) == :error
    end
  end
...

Link to this section Summary

Functions

Creates a batch of proves.

A macro to write simple a simple test shorter.

Link to this section Functions

Link to this macro

batch(description, list)

View Source (macro)

Creates a batch of proves.

The description is added to every prove in the batch.

Link to this macro

prove(description \\ "", expr)

View Source (macro)

A macro to write simple a simple test shorter.

Code like:

prove identity(5) == 5
prove "check:", identity(7) == 7

is equivalent to:

test "identity(5) == 5" do
  assert identity(5) == 5
end

test "check: indentity(7) == 7" do
  assert identity(7) == 7
end

prove supports the operators ==, !=, ===, !==, <, <=, >, >=, and =~.