Tools
Testing
@Test Functions, std.test's Expect and ExpectThrows, a package's tests/ folder, and doc tests.
Tests are Functions marked @Test, anywhere in a package, beside the code they test, where they
see its file’s Private names. fb test builds them and runs them, in
parallel, in one process. fb build and fb run leave them out.
@Test and std.test
A test takes nothing, and returns nothing or throws. Import std.test brings in:
Expect(condition) | On failure, prints the expression and each side’s value, and the test goes on to its next line |
ExpectThrows<E>(call) | Gives the E the call threw, or fails the test when it threw nothing |
Fail(message) | Fails the test |
Skip(message) | Ends the test as skipped |
A panic, an Assert that fails, or an error the test throws fails it too.
Import std.core
Import std.test
Public Class OutOfStock Extends Error
End Class
Function Take(stock: Int, count: Int): Int Throws OutOfStock
If count > stock Then Throw OutOfStock("only {stock} left")
Return stock - count
End Function
@Test
Function TakingLeavesTheRest() Throws OutOfStock
Expect(Try Take(5, 2) == 3)
Expect(Try Take(2, 2) == 0)
End Function
@Test
Function TakingTooManyThrows()
Local error = ExpectThrows<OutOfStock>(Take(1, 2))
Expect(error?.Message == "only 1 left")
End FunctionRunning 2 tests test main.TakingLeavesTheRest ... ok test main.TakingTooManyThrows ... ok test result: ok. 2 passed; 0 failed; 0 skipped; 0 filtered out; finished in 0.02s
A failing test
A failed Expect says where, what it tested, and each side’s value:
Running 1 test
test shop.stock.TakingLeavesTheRest ... FAILED
failures:
---- shop.stock.TakingLeavesTheRest ----
expect failed: Try Take(5, 2) == 4 at stock.fb:14:5
left: 3
right: 4
test result: FAILED. 0 passed; 1 failed; 0 skipped; 0 filtered out; finished in 0.02sfb exits with 1 when a test fails.
A test that panics doesn’t stop the others: fb starts the process again and runs the ones left. In a debug build, the memory a test leaks is reported with it.
A package’s tests/ folder
The .fb files of a package’s tests/ folder are tests that use the package from outside, as
another package would: through its Public names only. The package builds without them.
Doc tests
The examples under # Examples in doc comments are tests
too, each named after what it documents, as shop.prices.Price (example). fb test std runs the
standard library’s.
Choosing tests
fb test --filter <text> runs only the tests whose names have the text in them. A test’s name is
its module’s and its own, like shop.stock.TakingTooManyThrows.
Testing in CI runs them on every push.
Report an issue with this page or with FutureBlitz on GitHub.