Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Testing

This guide covers running and writing tests for job-orchestrator.

Running Tests

All Tests

cargo test

With Output

See println! output from tests:

cargo test -- --nocapture

Specific Test

# By name
cargo test test_upload

# By module
cargo test queue_dto::tests

Ignored Tests

No tests are marked #[ignore] at the moment. If you add slow tests that need extra setup, mark them with #[ignore] and run them with:

cargo test -- --ignored

Test Coverage

Using cargo-tarpaulin

Install:

cargo install cargo-tarpaulin

Generate coverage:

# HTML report
cargo tarpaulin --out Html --output-dir ./coverage

# XML report (for CI)
cargo tarpaulin --out Xml --output-dir ./coverage

View report:

open coverage/tarpaulin-report.html

Test Structure

Tests are inline #[cfg(test)] modules within the same file as the code they test:

src/
├── config/loader.rs          # Config loading and env var tests
├── controllers/server.rs     # Upload, download, terminate handler tests
├── controllers/client.rs     # Submit, retrieve, kill handler tests
├── models/status_dto.rs      # Status serialization tests
├── models/queue_dto.rs       # Quota and round-robin scheduling tests
├── services/server.rs        # Sender, getter, cleaner task tests
├── services/client.rs        # Runner, updater, cleaner task tests
└── utils/io.rs               # Script validator, file I/O tests

There is no separate tests/ directory — this is a binary crate with no public library interface.

Writing Tests

Unit Tests

#![allow(unused)]
fn main() {
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_something() {
        let result = function_under_test();
        assert_eq!(result, expected_value);
    }
}
}

Async Tests

Use tokio::test for async functions:

#![allow(unused)]
fn main() {
#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_async_function() {
        let result = async_function().await;
        assert!(result.is_ok());
    }
}
}

Mocking

Using mockito

For HTTP mocking — this is what the codebase actually uses (src/controllers/server.rs, src/services/client.rs, src/services/server.rs):

#![allow(unused)]
fn main() {
use mockito::Server;

#[tokio::test]
async fn test_http_client() {
    let mut server = Server::new_async().await;
    let mock = server
        .mock("GET", "/health")
        .with_status(200)
        .create_async()
        .await;

    // Test against server.url()

    mock.assert_async().await;
}
}

mockall is listed as a dev-dependency in Cargo.toml for trait-based mocking, but nothing in the codebase currently uses it — every existing test mocks HTTP boundaries with mockito instead.

Test Utilities

Test Fixtures

Create reusable test data:

#![allow(unused)]
fn main() {
#[cfg(test)]
mod test_utils {
    pub fn create_test_job() -> Job {
        let mut job = Job::new("/tmp/test-data");
        job.set_user_id(1);
        job.set_service("test".to_string());
        job
    }
}
}

Temporary Directories

Use tempfile for temporary test directories:

#![allow(unused)]
fn main() {
use tempfile::TempDir;

#[test]
fn test_file_operations() {
    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("test.txt");

    // Test file operations

    // TempDir is automatically cleaned up
}
}

CI Testing

Tests run automatically via Forgejo Actions (.forgejo/workflows/ci.yml), in a single ci job with two steps:

  • Run clippy — cargo clippy -- -D warnings
  • Run tests with coverage — cargo tarpaulin --engine llvm

There’s no separate cargo test step — tarpaulin runs the test suite while measuring coverage.

Linting

Clippy

Run Clippy for additional checks:

cargo clippy -- -D warnings

Common fixes:

  • #[allow(clippy::lint_name)] to suppress specific lints
  • Configure in clippy.toml or Cargo.toml

Formatting

Check formatting:

cargo fmt -- --check

Fix formatting:

cargo fmt

Debugging Tests

With println

#![allow(unused)]
fn main() {
#[test]
fn test_debug() {
    let value = compute_something();
    println!("Debug value: {:?}", value);  // Use --nocapture to see
    assert!(value.is_valid());
}
}

With RUST_BACKTRACE

RUST_BACKTRACE=1 cargo test

See Also