A polite, well mannered and thoroughly upstanding testing framework for Elixir
A polite, well mannered and thoroughly upstanding testing framework for Elixir.
Add to your mix.exs
defp deps do [ {:amrita, "~>0.4", github: "josephwilk/amrita"} ] end
After adding Amrita as a dependency, to install please run:
mix deps.get
Ensure you start Amrita in: test/test_helper.exs ```elixir Amrita.start
Amrita.start(formatter: Amrita.Formatter.Documentation) ```
Amrita.Sweetwhich will bring in everything you need to use Amrita:
Code.require_file "../test_helper.exs", __ENV__.filedefmodule ExampleFacts do use Amrita.Sweet
fact "addition" do 1 + 1 |> 2 end end
Run your tests through mix:
$ mix amrita # Run all your tests$ mix amrita test/integration/t_mocks.ex # Run a specific file
$ mix amrita test/integration/t_mocks.ex:10 # Run a specific test at a line number
$ mix amrita --trace # Show execution time for slow tests
Now time to write some tests!
Amrita supports BDD style mocks.
Examples:
defmodule Polite do def swear? do false enddef swear?(word) do false end end
fact "mock with a wildcard" do provided [Polite.swear? |> true] do Polite.swear? |> truthy end end
fact "mock with a wildcard" provided [Polite.swear?(_) |> true] do Polite.swear?(:yes) |> truthy Polite.swear?(:whatever) |> truthy end end
fact "mock with a matcher function" do provided [Polite.swear?(fn arg -> arg =~ ~r"moo") |> false] do Polite.swear?("its ok to moo really") |> falsey end end
fact "mock with return based on argument" do provided [Polite.swear?(:pants) |> false, Polite.swear?(:bugger) |> true] doFunk.swear?(:pants) |> falsey Funk.swear?(:bugger) |> truthy
end end
Amrita is also all about checker based testing!
Code.require_file "../test_helper.exs", __ENV__.filedefmodule ExampleFacts do use Amrita.Sweet
facts "about Amrita checkers" do
fact "`equals` checks equality" do 1 - 10 |> equals -9 # For convience the default checker is equals # So we can write the above as 1 - 10 |> -9 # Pattern matching with tuples { 1, 2, { 3, 4 } } |> equals {1, _, { _, 4 } } # Which is the same as { 1, 2, { 3, 4 } } |> {1, _, { _, 4 } } end fact "contains checks if an element is in a collection" do [1, 2, 4, 5] |> contains 4 {6, 7, 8, 9} |> contains 9 [a: 1, :b 2] |> contains {:a, 1} end fact "! negates a checker" do [1, 2, 3, 4] |> !contains 9999 # or you can add a space, like this. Whatever tickles your fancy. [1, 2, 3, 4] |> ! contains 9999 10 |> ! equal 11 end fact "contains works with strings" do "mad hatters tea party" |> contains "hatters" "mad hatter tea party" |> contains ~r"h(\w+)er" end fact "has_prefix checks if the start of a collection matches" do [1, 2, 3, 4] |> has_prefix [1, 2] {1, 2, 3, 4} |> has_prefix {1, 2} "I cannot explain myself for I am not myself" |> has_prefix "I" end fact "has_prefix with a Set ignores the order" do {1, 2, 3, 4} |> has_prefix Set.new([{2, 1}]) end fact "has_suffix checks if the end of a collection matches" do [1, 2, 3, 4 ,5] |> has_suffix [4, 5] {1, 2, 3, 4} |> has_suffix {3, 4} "I cannot explain myself for I am not myself" |> has_suffix "myself" end fact "has_suffix with a Set ignores the order" do {1, 2, 3, 4} |> has_suffix Set.new([{4, 3}]) end fact "for_all checks if a predicate holds for all elements" do [2, 4, 6, 8] |> for_all even(&1) # or alternatively you could write [2, 4, 6, 8] |> Enum.all? even(&1) end fact "odd checks if a number is, well odd" do 1 |> odd end fact "even checks is a number if even" do 2 |> even end fact "roughly checks if a float within some +-delta matches" do 0.1001 |> roughly 0.1 end fact "falsey checks if expression evalulates to false" do nil |> falsey end fact "truthy checks if expression evaulates to true" do "" |> truthy end defexception Boom, message: "Golly gosh" fact "raises checks if an exception was raised" do fn -> raise Boom end |> raises ExampleFacts.Boom end
end
future_fact "I'm not run yet, just printed as a reminder. Like a TODO" do # Never run false |> truthy end
fact "a fact without a body is much like a TODO"
Backwards compatible with ExUnit
test "arithmetic" do assert 1 + 1 == 2 end
end
The syntax for assertions is as follows:
# Equality check ACTUAL |> [EXPECTED] # Not equal check ACTUAL |> ! [EXPECTED]Using a checker function
ACTUAL |> CHECKER [EXPECTED]
or negative form
ACTUAL |> !CHECKER [EXPECTED]
Its simple to create your own checkers:
defchecker a_thousand(actual) do rem(actual, 1000) |> equals 0 endfact "about 1000s" do 1000 |> a_thousand # true 1200 |> ! a_thousand # true end
Amrita tries its best to be polite with its errors:
Checkout an example using Amrita with Dynamo: https://github.com/elixir-amrita/amritawithdynamo
See the wiki for various IDE plugins for Amrita: https://github.com/josephwilk/amrita/wiki/Plugins
Hacking on Amrita.
Amrita runs tests against Elixir's latest stable release and against Elixir master. Make is your friend for running these tests:
# Run lastest stable and elixir master make ciRun tests against your current Elixir install
make
http://josephwilk.github.io/amrita/docs
Thanks for reading me, I appreciate it.
Have a good day.
Maybe drink some tea.
Its good for the constitution.
(The MIT License)
Copyright (c) 2014-2016 Joseph Wilk
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.