Overview
Magpie uses Rust’s built-in testing framework with additional tools for mocking external services. Tests are co-located with the code they test in#[cfg(test)] modules.
Running Tests
Basic Test Commands
Building with Features
Test Structure
Tests are written inline using#[cfg(test)] modules:
Test Organization
- Unit tests: In-file
#[cfg(test)]modules at the bottom of each source file - Integration tests: Would go in
tests/directory (not currently used) - Test helpers: Shared test utilities in
#[cfg(test)]modules
Ignored Tests
Tests that require external services (Claude CLI, GitHub CLI, Daytona API) are marked with#[ignore]:
#[ignore]:
- Requires
claudeCLI authenticated - Requires
ghCLI authenticated - Requires
DAYTONA_API_KEYenvironment variable - Requires network access
- Long-running or expensive operations
Running Ignored Tests
MockSandbox for Testing
TheMockSandbox allows you to test code that depends on command execution without actually running commands:
MockSandbox Methods
MockSandbox File Operations
Testing with wiremock
For testing HTTP integrations (Plane API, Teams webhooks), usewiremock to mock HTTP servers:
Common wiremock Patterns
Test Helpers and Utilities
Creating Test Configurations
Temporary Directories
Git Test Repositories
Assertions
Basic Assertions
Assertions with Context
Testing Errors
Testing Best Practices
1. Test Names Should Be Descriptive
2. Test One Thing at a Time
3. Use Test Helpers for Setup
4. Test Error Cases
5. Avoid Flaky Tests
Test Coverage
To check test coverage, you can usecargo-tarpaulin:
Continuous Integration
Tests run automatically in CI on every PR. The CI pipeline:- Runs
cargo fmt --check(formatting) - Runs
cargo clippy -- -D warnings(lints) - Runs
cargo test(tests, excluding#[ignore]) - Runs
cargo build(compilation check)
Examples from the Codebase
Example: Testing the slugify Function
Fromcrates/magpie-core/src/git.rs:257:
Example: Testing with MockSandbox
Fromcrates/magpie-core/src/sandbox/mock.rs:122:
Example: Testing with wiremock
Fromcrates/magpie-core/src/plane.rs:192:
Summary
- Use
cargo testfor basic test runs - Mark tests requiring external services with
#[ignore] - Use
MockSandboxfor testing command execution - Use
wiremockfor testing HTTP integrations - Write focused tests with descriptive names
- Test both success and error cases
- Add helpful assertion messages