Name: elm-test
Owner: Elm Community
Description: Write unit and fuzz tests for your Elm code, in Elm.
Created: 2016-04-28 02:45:17.0
Updated: 2018-05-24 21:34:01.0
Pushed: 2018-04-15 21:12:58.0
Homepage: http://package.elm-lang.org/packages/elm-community/elm-test/latest
Size: 782
Language: Elm
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Write unit and fuzz tests for your Elm code, in Elm.
Here are three example tests:
e : Test
e =
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 uses a few common functions:
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 inputsCheck out a large real-world test suite for more.
There are several ways you can run tests locally:
npm install -g elm-test
Here's how to set up and run your tests using the CLI test runner:
npm install -g elm-test
if you haven't already.cd
into the project's root directory that has your elm-package.json
.elm-test init
. It will create a tests
directory inside this one,
with some files in it.elm-package.json
into
tests/elm-package.json
. These dependencies need to stay in sync, so make
sure whenever you change your dependencies in your current
elm-package.json
, you make the same change to tests/elm-package.json
.elm-test
.tests/Example.elm
to introduce new tests.Here are some examples of running tests on CI servers:
During development, you'll often want to focus on specific tests, silence failing tests, or jot down many ideas for tests that you can't implement all at once. We've got you covered with skip
, only
, and todo
:
uite : Test
uite =
describe "skip, only, and todo"
[ only <| describe "Marking this test as `only` means no other tests will be run!"
[ test "This test will be run" <|
\_ -> 1 + 1 |> Expect.equal 2
, skip <| test "This test will be skipped, even though it's in an only!" <|
\_ -> 2 + 3 |> Expect.equal 4
]
, test "This test will be skipped because it has no only" <|
\_ -> "left" |> Expect.equal "right"
, todo "Make sure all splines are reticulated"
]
If you run this example, or any suite that uses one of these three functions, it will result in an incomplete test run. No tests failed, but you also didn't run your entire suite, so we can't call it a success either. Incomplete test runs are reported to CI systems as indistinguishable from failed test runs, to safeguard against accidentally committing a gutted test suite!
There are a few extra ideas that apply to testing webapps and reusable view packages:
Main
module. Most of your code belongs in other modules, so import those instead.Make sure you grab the latest versions of the test runner that you are using:
npm update -g elm-test
elm package install rtfeldman/html-test-runner
Fuzz.frequency
now fails the test if the frequency is invalid, rather than return a Result
. If you are using this function, you can remove your Err
handling code. More likely you are using Fuzz.frequencyOrCrash
, which you can replace with Fuzz.frequency
.
Instead of using Test.filter
to avoid running tests, use skip
and only
(see above for documentation).
We now forbid tests and suites to have descriptions that are blank, or that are identical across siblings or parents and children. If you get failures from this, rename your tests to be clearer about what they're testing.
You will need to delete elm-stuff
and tests/elm-stuff
.
If you are using the Node runner, you will need to install the latest version (npm update -g elm-test
) and pull down the new Main.elm
: curl -o tests/Main.elm https://raw.githubusercontent.com/rtfeldman/node-test-runner/3.0.1/templates/Main.elm
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.
| Version | Notes |
| ——- | —– |
| 4.0.0 | Add only
, skip
, todo
; change Fuzz.frequency
to fail rather than crash on bad input, disallow tests with blank or duplicate descriptions.
| 3.1.0 | Add Expect.all
| 3.0.0 | Update for Elm 0.18; switch the argument order of Fuzz.andMap
.
| 2.1.0 | Switch to rose trees for Fuzz.andThen
, other API additions.
| 2.0.0 | Scratch-rewrite to project-fuzzball
| 1.0.0 | ElmTest initial release