Write unit and fuzz tests for your Elm code, in Elm.
Here are three example tests:
suite : Test
suite =
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:
describe
to add a description string to a list of teststest
to write a unit testExpect
to determine if a test should pass or failfuzz
to run a function that produces a test several times with randomly-generated inputs
Check out a more complete example or a large real-world test suite for more.
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
Here's how set up and run your tests using the CLI test runner.
- Run
npm install -g elm-test
if you haven't already. cd
into the directory that has yourelm-package.json
.- Run
elm-test init
. It will create atests
directory inside this one, with some files in it. - Copy all the dependencies from
elm-package.json
intotests/elm-package.json
. These dependencies need to stay in sync, so make sure whenever you change your dependencies in your currentelm-package.json
, you make the same change totests/elm-package.json
. - Run
elm-test
.
Edit Tests.elm
to introduce new tests.
Happy testing!
Here are some examples of running tests on CI servers:
legacy-elm-test
provides a
drop-in replacement for the ElmTest 1.0
API, except implemented in terms of
the current elm-test
. It also includes support for elm-check
tests.
This lets you use the latest test runners right now, and upgrade incrementally.