View Source Prove (Prove v0.1.7)

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

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

The disadvantage of these macros is that the tests are containing fewer descriptions. For this reason and also if a prove looks too complicated, a regular test is to prefer.

example

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 test/num_test.exs --trace --seed 0

NumTest [test/num_test.exs]
  * prove check/1 (1) (0.00ms) [L#20]
  * prove check/1 returns :odd or :even (1) (0.00ms) [L#23]
  * prove check/1 returns :odd or :even (2) (0.00ms) [L#24]
  * prove check/1 returns :odd or :even for big num (1) (0.00ms) [L#25]
  * prove check/1 returns :error (1) (0.00ms) [L#29]
  * prove check/1 returns :error (2) (0.00ms) [L#30]


Finished in 0.08 seconds (0.00s async, 0.08s sync)
6 proves, 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 or :even" do
      assert Num.check(1) == :odd
      assert Num.check(2) == :even
      assert Num.check(2_000) == :even
    end

    test "returns :error" do
      assert Num.check("1") == :error
      assert Num.check(nil) == :error
    end
  end
...
$> mix test test/num_test.exs --trace --seed 0

NumTest [test/num_test.exs]
  * test check/1 returns :zero (0.00ms) [L#36]
  * test check/1 returns :odd or :even (0.00ms) [L#40]
  * test check/1 returns :error (0.00ms) [L#46]


Finished in 0.03 seconds (0.00s async, 0.03s sync)
3 tests, 0 failures

Randomized with seed 0

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.

A batch adds the description to every prove. This can be used to groupprovess with a context. Every prove is still an own test.

Code like:

batch "valid" do
  prove 1 == 1
  prove "really", 2 == 2
end

is equivalent to:

test "valid (1)" do
  assert 1 == 1
end

test "valid really (1)" do
  assert 2 == 2
end
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 identity(6) > 5
prove "check:", identity(7) == 7

is equivalent to:

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

test "(2)" do
  assert identity(6) > 5
end

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

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