TE

Mojo Testing

Mojo testing conventions using the built-in testing module

Details

Language / Topic
mojoMojo
Category
Testing

Rules

balanced
- Import `from testing import assert_equal, assert_true, assert_false` and use these assertion functions instead of bare `if` checks in tests.
- Name test functions with the `test_` prefix so the Mojo test runner discovers them automatically.
- Isolate each test function to a single behavior — one assertion per logical concept, not one assertion per file.
- Use `assert_almost_equal(a, b, atol=1e-6)` for floating-point comparisons — never use `==` on `Float32` or `Float64` values in tests.
- Place tests in a `tests/` directory mirroring the source tree — a file `src/math.mojo` gets `tests/test_math.mojo`.
- Test both `fn` and `def` code paths separately when a module exposes both — they have different type-checking and error semantics.
- Use `@parameter` test variants to validate behavior across multiple compile-time constants without duplicating test logic.