std.test
A simple unit testing library.
This module provides a simple and opinionated unit testing library. Tests are
simply closures with a description, and define assertions using a Test
instance passed as an argument to these closures.
Unlike many other testing libraries, assertion failures don't immediately terminate the test, instead the test runs until the end. This ensures all failing assertions are immediately visible, instead of only the first assertion per failing test being visible.
This module doesn't provide the means for setting up hooks (e.g. a function that runs before every or all tests), nor does it provide the means to tag tests, temporarily disable them, and more.
Tests are run concurrently and in random order. The number of concurrently running tests is limited, with the default limit being the number of CPU cores available. This makes it easier to interact with external services, such as databases, as these may only be able to handle a limit number of concurrent requests.
Writing tests
To write a test you need to import the Tests
type into your module of
choice. You can then write your tests like so:
import std.test (Tests)
class async Main {
fn async main {
let tests = Tests.new
tests.test('This is the test description') fn (t) {
t.equal('foo'.size, 3)
}
tests.run
}
}
The closure passed to the test
method must be a uni
closure, otherwise it
can't be sent to the process that runs the test. If you pass the closure
directly (as shown above) no extra work is needed. If you decide to first
store the closure in a variable, you must explicitly recover it:
import std.test (Tests)
class async Main {
fn async main {
let tests = Tests.new
let block = recover fn (t: mut Tets) { t.equal('foo'.size, 3) }
tests.test('This is the test description', block)
tests.run
}
}
For more information about the available assertions, refer to the
documentation of the Test
type.
Traits
Reporter | A type used for reporting test progress. |
Classes
Failure | A test failure produced by an assertion. | |
Filter | A type describing how to filter out tests. | |
Output | The output of a sub process. | |
Plain | A test reporter that prints results in a simple text based format. | |
Process | A child process to run as part of a unit test. | |
Test | A single unit test. | |
Tests | A collection of tests to run. |