How to write parameterized tests in Elm

If you want different Expectations, then you’ll need to have different tests.

suite : Test
suite =
    describe "isOdd" <|
        List.map
            (\{ given, expected } ->
                test (String.fromInt given) <|
                    \_ ->
                        Expect.equal expected (isOdd given)
            )
            [ { given = 1, expected = True }
            , { given = 2, expected = False }
            , { given = 3, expected = True }
            , { given = 6, expected = False }
            ]

This pattern is similar to “table driven tests” that Go community prefers, but because of the way List.map argument order works, your test code will come before your test data “table”; no biggie imo

Also, feel free to use Debug.toString given instead of String.fromInt given if your given is a more complicated type. Since this is just test, using Debug to spell out the test name is probably forgivable.

3 Likes