Docs · Standard library
@testing
@testing holds the functions a test file is written with: describe and
it for structure, expect and its matchers for checks, and beforeEach and
afterEach for setup. vyi test runs the files and prints the report. Guide
16, Testing teaches them, and the
CLI reference covers vyi test.
import { describe, it, expect } from "@testing"
import { beforeEach, afterEach, failures } from "@testing"
import { Expectation } from "@testing"Exports
| Name | Kind | Summary |
|---|---|---|
describe | function | a named group of tests |
it | function | one named test |
expect | function | opens a check on a value |
Expectation(T) | type | what expect returns; the matchers are its methods |
toBe, toEqual, toBeTrue, toBeFalse | methods on Expectation(T) | the matchers |
beforeEach, afterEach | functions | a closure run before or after each following test |
failures | function | the number of failed checks so far |
Test files
A test file's name ends in .tests.vyi. It holds declarations and top-level
calls such as describe(...) and it(...), and no main. vyi test adds a
main that makes the top-level calls in the order they are written and then
exits with failures(), capped at 255. A test file that declares its own
main is run as it is.
Everything runs in one program, in order: there is no isolation between
tests, and a file-level mut variable keeps its value from one test to the
next.
import { describe, it, expect } from "@testing"
fn discount(total: i32) i32 {
if total >= 100 { return 10 }
return 0
}
describe("discount", () => {
it("is 10 from 100", () => {
expect(discount(150)).toBe(10)
})
it("is 0 below 100", () => {
expect(discount(99)).toBe(0)
})
})describe and it
| Function | Signature | Does |
|---|---|---|
describe | fn describe(name: [*con]u8, body: fn()) | runs body at once, one level deeper; the tests inside are reported under name |
it | fn it(name: [*con]u8, body: fn()) | runs the beforeEach closure, body, and the afterEach closure, then reports the test |
- A test passes when no check inside
bodyfailed. A failed check does not stop the test; the rest ofbodyruns. - A failed test points at its last failed check, as
file:line:columnof theexpectcall. describecalls nest to any depth. The report names a test by its path, as indiscount > is 10 from 100.bodyis a closure with no parameters and no result (guide 04).- A check made outside any
itcounts infailures()but belongs to no test. When it fails, the file fails with no failing test to show. - A panic stops the whole file: the tests after it do not run, and the file is reported as failed.
expect and the matchers
fn expect(actual: _) Expectation(typeof actual) takes a value of any type.
Expectation(T) is struct { pub actual: T }, and the matchers are its
methods:
| Matcher | Signature | Passes when |
|---|---|---|
toBe | fn (e: Expectation(T)) toBe(expected: T) | actual == expected |
toEqual | fn (e: Expectation(T)) toEqual(expected: T) | actual == expected; the same check as toBe |
toBeTrue | fn (e: Expectation(T)) toBeTrue() | actual == true; T must be bool |
toBeFalse | fn (e: Expectation(T)) toBeFalse() | actual == false; T must be bool |
expectedhas the same type asactual. A literal takes that type, soexpect(i64(5)).toBe(5)compiles;expect(5).toBe(i64(5))is reported asargument 1 has type i64, expected i32.Tneeds==. Numbers,bool,char,[*con]u8(by content),String, enums, optionals, and results have it. A struct needs an@eqmethod (operators); without one,toBeis reported inside@testingas `operator==is not implemented for type Point`.toBeTrue()on a non-boolvalue is reported inside@testingas `this operand of==has type bool, where i32 is expected(for ani32`).- A failure does not print the values. Only the location is reported.
import { it, expect } from "@testing"
enum StockError { OutOfStock }
fn reserve(inStock: i32, wanted: i32) StockError!i32 {
if wanted > inStock { return Err(OutOfStock) }
return Ok(inStock - wanted)
}
it("reserves what is in stock", () => {
expect(reserve(5, 2)).toBe(Ok(3))
expect(reserve(1, 2)).toEqual(Err(OutOfStock))
expect(reserve(5, 5) == Ok(0)).toBeTrue()
})beforeEach and afterEach
| Function | Signature | Does |
|---|---|---|
beforeEach | fn beforeEach(body: fn()) | sets the closure that every following it runs before its body |
afterEach | fn afterEach(body: fn()) | sets the closure that every following it runs after its body |
- There is one of each at a time. A second
beforeEachreplaces the first; it does not add to it. AbeforeEachin a nesteddescribereplaces the outer one for the tests in that group. - A hook ends with its
describe. When adescribereturns, the hooks that were set before it apply again. Tests in a nesteddescribeuse the enclosing group's hooks until the group sets its own. - A hook set at the top level applies to every following test outside a group that sets its own.
- There is no
beforeAllorafterAll: write that code before or after the calls it surrounds.
import { describe, it, expect, beforeEach } from "@testing"
mut cartItems = 0
describe("cart", () => {
beforeEach(() => { cartItems = 0 })
it("adds an item", () => {
cartItems += 1
expect(cartItems).toBe(1)
})
it("starts empty", () => {
expect(cartItems).toBe(0)
})
})failures
fn failures() i32 returns how many checks have failed so far in the program,
across every test. The main that vyi test adds exits with it. A test file
with its own main returns it to report failures the same way:
import { it, expect, failures } from "@testing"
fn main() !u8 {
it("adds", () => {
expect(2 + 3).toBe(5)
})
return Ok(u8(failures()))
}See also: guide 16, Testing, the
CLI reference for vyi test, and
@trace, which @testing uses to find a failed check's location.