Writing tests
Module weaver.test: its API, wv doc weaver.test.
A test is a function marked @test, in the file it tests, whose names it
sees, private ones included. wv test compiles and runs the tests of the
program and of every module of its package; wv build leaves them out.
import weaver.test.{assert, assertEq}
@test("parses negative numbers")fn parsesNegative() ! { assertEq(Int.parse("-7")!, -7)}A test fails on a false assertion, on a panic, or on an error it lets
out: declare it ! and propagate with !, the error’s message is
reported. A file named tests.wft, or ending in _tests.wft, holds what
only the tests use, helpers and types, and is left out of the programs that
import the module.
Assertions
Section titled “Assertions”assert(condition, message): the message is computed only if the condition is false.assertEq(actual, expected)andassertNe(actual, other), which show both values when they fail.assertErr(result): the error of a result the test expects to fail.
Each reports where it is written. A doc’s # Examples blocks are tests
too, named after what they document, doc of Path.join, example 1, and
see assert and assertEq without importing them.
What a test expects
Section titled “What a test expects”@expectPanic("out of bounds")passes if the test panics with a message containing the text, any panic without one.@expectExit(3)passes if it callsProcess.exit(3).@skip("reason")leaves it out;skip("no network"), called while it runs, stops it there. A skipped test is neither passed nor failed.
Tables of cases
Section titled “Tables of cases”@cases(table) runs the test once per element, a tuple binding its
parameters in order. The description interpolates them, so that each case
is a test of its own, with its line in the report:
@test("parses ${text}")@cases([("42", 42), ("-7", -7), ("0x1f", 31)])fn parses(text: String, want: Int) ! { assertEq(Int.parse(text)!, want)}The table is computed when the tests start, so it may come from a call,
@cases(loadCases()). wv test -list prints every test and case without
running them.
Suites
Section titled “Suites”A type marked @suite groups tests that share a value: built once by its
new, disposed of after its last test, its methods marked @beforeEach
and @afterEach run around each. Its tests are its methods marked
@test, run in order in one process.
@suite("store")type StoreTests = { db: Db }
impl StoreTests { fn new(): StoreTests ! IoError = StoreTests{db: Db.open()!}
@beforeEach fn reset(mut self) { self.db.clear() }
@test("inserts") fn inserts(mut self) { self.db.insert(row) }}
impl Disposable for StoreTests { fn dispose(sink self) { self.db.close() }}A suite that cannot be built fails each of its tests with the reason.
@parallel on a suite whose tests only read self lets them run at once.
Running them
Section titled “Running them”wv testruns the tests in as many processes as cores, each behind a barrier: a panic or an exit fails its test alone.-run textkeeps the tests whose name contains the text; what a test prints is shown when it fails, and for every test with-v.- Each test has a minute by default,
-timeout 30sto change it,0for none;@timeout(5.seconds())sets one test’s or one suite’s. wv test weaver.timeruns a module of std,wv test corethe examples of the prelude’s docs.-covermeasures the lines and branches the tests run;-coverprofile filewrites them for an editor or a CI.-report junit.xmlwrites the results in JUnit’s format, which GitLab and GitHub show beside a pipeline.
Tests of one process share its globals, its environment and its current
directory: a test restores what it changes, Env.set or the current
directory, with defer or in an @afterEach.