Testing Guide
How to write and run tests for Nexus OS.
Running Tests
# Run all tests
cargo test
# Run a specific test
cargo test test_agent_lifecycle
# Run tests with output
cargo test -- --nocapture
# Run only integration tests
cargo test --test integration
Writing Unit Tests
Unit tests go in the same file as the code they test:
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn test_agent_creation() {
let dir = TempDir::new().unwrap();
let store = Store::open(dir.path()).unwrap();
store.create_agent("test", "idle", "test.wasm").unwrap();
let agents = store.list_agents().unwrap();
assert_eq!(agents.len(), 1);
assert_eq!(agents[0].name, "test");
}
}
Writing Integration Tests
Integration tests go in the tests/ directory:
// tests/integration.rs
use std::process::Command;
#[test]
fn test_cli_init() {
let dir = tempfile::TempDir::new().unwrap();
let output = Command::new("cargo")
.args(["run", "--", "init"])
.current_dir(dir.path())
.output()
.unwrap();
assert!(output.status.success());
}
Test Coverage
Generate coverage reports:
cargo install cargo-tarpaulin
cargo tarpaulin --out html
open tarpaulin-report.html