Test Organization
In this chapter, we'll explore organize unit tests and integration tests effectively.
Test Organization
The Mana community thinks about tests in two categories: unit tests and integration tests.
Unit Tests
Unit tests are small, focused, and test one module in isolation. Place them in the same file as the code they test:
fn add_two(a: int) -> int {
return internal_adder(a, 2)
}
fn internal_adder(a: int, b: int) -> int {
return a + b
}
#[cfg(test)]
module tests {
use super::*
#[test]
fn internal() {
assert_eq(4, internal_adder(2, 2))
}
}The #[cfg(test)] annotation tells Mana to compile and run the test code only with mana test.
Testing Private Functions
Mana allows testing private functions:
fn internal_adder(a: int, b: int) -> int {
return a + b
}
#[cfg(test)]
module tests {
use super::*
#[test]
fn test_internal() {
assert_eq(internal_adder(2, 2), 4)
}
}Integration Tests
Integration tests are external to your library. Create a tests directory:
my_project/
├── project.mana
├── src/
│ └── lib.mana
└── tests/
└── integration_test.mana
tests/integration_test.mana:
use my_project
#[test]
fn it_adds_two() {
assert_eq(4, my_project::add_two(2))
}Each file in tests/ is a separate crate. Run a specific integration test file:
mana test --test integration_testSubmodules in Integration Tests
Create shared helper code in tests/common/mod.mana:
tests/
├── common/
│ └── mod.mana
└── integration_test.mana
tests/common/mod.mana:
pub fn setup() {
// Setup code
}tests/integration_test.mana:
use my_project
module common
#[test]
fn it_adds_two() {
common::setup()
assert_eq(4, my_project::add_two(2))
}Summary
Mana's testing features help you specify how code should behave. Unit tests exercise different parts of a library separately and can test private implementation details. Integration tests check that parts of the library work together correctly.
Explore the Appendix for reference material.