Test
Testing
Suites that selm test runs.
Put the module in tests/ and expose a value named tests. selm test runs every suite. Pass a path to run one file. Append :line to run the test or describe that starts on that line. A describe line runs every test in the group.
selm test
selm test tests/GreetingTest.selm
selm test tests/GreetingTest.selm:14A suite is a describe. The second argument is context shared by the tests in that group. Use () when they do not need any. Each test receives that context and returns an Expectation from Expect. The runner prints the group and the case, such as Greeting > says hello.
module GreetingTest exposing (tests)
import Expect
import Test exposing (Test, describe, test)
hello : String -> String
hello name =
"Hello, " ++ name
tests : Test ()
tests =
describe "Greeting"
()
[ test "says hello"
<| (\_ctx -> Expect.equal "Hello, Ada" (hello "Ada"))
]Database work goes in sandbox, which rolls the transaction back. captureLog records Debug.inspect. Channels are exercised with Channel.Test, which joins and pushes without a browser. Working suites live in examples/02_counter, examples/03_todo_app, and examples/05_chat.
Types
Test
type Test ctx = TestA test or a group of tests.
ctx is the value describe passes into each test. The suite you expose is Test (), which is what describe returns.
Examples
tests : Test ()Values
describe
describe : String -> ctx -> List (Test ctx) -> Test ()Group tests that share a context value.
Each test in the list receives that value. () is the usual context when the tests only call the functions under test. Pass a record when several cases share setup.
Examples
describe "Greeting"
{ name = "Ada" }
[ test "uses the name"
<| (\person -> Expect.equal "Hello, Ada" ("Hello, " ++ person.name))
]test
test : String -> (ctx -> Expectation) -> Test ctxOne case. The function receives the describe context and returns an Expectation.
Ignore () with \_ctx. Name the argument when the context has fields, as in the describe example. A test returns one expectation. Combine boolean checks with && inside Expect.true when a case needs more than one fact.
Examples
test "says hello"
<| (\_ctx -> Expect.equal "Hello, Ada" (hello "Ada"))sandbox
sandbox : Transaction Expectation -> ExpectationRun a Transaction and always roll it back.
The transaction's value is an Expectation. A failed transaction becomes Expect.fail, so the test fails with that error. The rollback finishes before the next test runs, so these writes do not leak.
Examples
test "register then find"
<| (\_ctx ->
Test.sandbox
(Users.register "ada@example.com" "ada" "password123"
|> Transaction.andThen (\_ -> Users.findByUsername "ada")
|> Transaction.map (\user -> Expect.equal "ada@example.com" user.email)
)
)See examples/03_todo_app/tests/TodosTest.selm and examples/04_conduit/conduit-api/tests/ConduitTest.selm.
captureLog
captureLog : (() -> a) -> (String, a)Run a function and collect what Debug.inspect printed.
You get the log text and the function's return value.
Examples
test "logs the greeting"
<| (\_ctx ->
let
captured =
Test.captureLog (\() -> Debug.inspect "greeting" "hello")
in
Expect.true (String.contains "greeting" (Tuple.first captured))
)