View Source Criterion (Criterion v0.1.11)
A frame work to write unit tests as a list of steps. It can be used to write tests BDD style.
Usage
- Define a feature using
feature/2
macro - Define scenarios under the feature using the
scenario/2
macro. - Inside each scenario, define steps using
step/2
block. - Steps can be either plain steps, steps with context variables or shared step
- Shared steps can be defined using
defstep/4
macro
Example
Shared steps
defmodule Criterion.SharedSteps do
import Criterion
defstep "Given a number", _context, args do
min = args[:min] || 0
%{number: min + Enum.random(0..100)}
end
end
Test
defmodule CriterionTest do
use ExUnit.Case
import Criterion
alias Criterion.SharedSteps
feature "Math" do
setup do
{:ok, pi: 3.14}
end
scenario "Square" do
step("Given a number greater than 5",
from: SharedSteps, # use only if the reusable step is in another module
via: "Given a number" # use only if the reusable step has a different step name,
where: [min: 5] # use only when you want to pass arguments to the reusable step,
)
step "When the number is multiplied by it self", %{number: number} do
result = number * number
%{result: result} # will be merged to the test context
end
step "Then the result is greater than the number", %{result: result, number: number} do
assert result > number
end
# you can access data from the initial context of the test
step "And pi is a constant", %{pi: pi} do
assert pi == 3.14
end
end
end
end