Write tests in Elm!
Let's start with some example code:
test : Test test = describe "The String module" [ describe "String.reverse" -- Nest as many descriptions as you like. [ test "has no effect on a palindrome" <| \() -> let palindrome = "hannah" in Expect.equal palindrome (String.reverse palindrome) -- Expect.equal is designed to be used in pipeline style, like this. , test "reverses a known string" <| \() -> "ABCDEFG" |> String.reverse |> Expect.equal "GFEDCBA" -- fuzz runs the test 100 times with randomly-generated inputs! , fuzz string "restores the original string if you run it again" <| \randomlyGeneratedString -> randomlyGeneratedString |> String.reverse |> String.reverse |> Expect.equal randomlyGeneratedString ] ]
This code includes a few common things:
describeto add a description string to a list of tests
testto write a unit test
Expectto determine if a test should pass or fail
fuzzto run a test several times with randomly-generated inputs.
Running tests locally
There are several ways you can run tests locally:
from your terminal via
npm install -g elm-test
- from your browser
- using a custom runner of your own design
Running tests on CI
Here are some examples of running tests on CI servers: