repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

opencode/skills/testing-rust/SKILL.md

Raw

name: testing-rust description: Organize and optimize Rust test suites — unit tests in src/, integration tests in tests/, #[cfg(test)] vs external mod, doctest disable, compile time reduction. license: MIT

What I do

Guide test organization in Rust/Cargo projects for maximum performance.

How Cargo Tests Work

Unit Tests

Located in src/. Cargo compiles with --cfg test and runs as single executable.

Integration Tests

Located in tests/. Each file is a separate binary that re-links the library.


Performance

Compilation

Integration tests re-link library with each binary.

Example: Cargo consolidated integration tests → 3x faster compilation, 5x smaller disk

Runtime

Example: Consolidated several integration tests → 20s → 13s runtime


Organization Rules

For Libraries (Published)

Use one integration test called it:

tests/
  it.rs

# Or for larger crates
tests/it/
  main.rs
  foo.rs

Why: Forces using public API like consumers do.

For Internal Libraries

Avoid integration tests. Use unit tests in tests.rs:

src/
  lib.rs
  tests.rs       # Contains: #[cfg(test)] mod tests;
  tests/
    foo.rs       # With: mod tests;

Benefits: Access private API, faster compile, no re-linking.


Tricks

Disable Doc Tests

Doc tests are slow—each linked as separate binary.

[lib]
doctest = false

External Test Module

// Instead of:
#[cfg(test)]
mod tests { /* tests here */ }

// Use:
#[cfg(test)]
mod tests;  // In tests.rs file

Why: Modifying tests won't recompile the library.

Single Test Crate (Maximum Performance)

# In each crate
[lib]
test = false

Create one workspace test crate that links everything.


Summary

  1. One integration test: tests/it.rs
  2. Prefer unit tests: Access private API, faster
  3. Disable doc tests: In large internal projects
  4. External test module: mod tests; in separate file
  5. Consider workspace test crate: For maximum speed
Markdown source
File outline
---
name: testing-rust
description: Organize and optimize Rust test suites — unit tests in src/, integration tests in tests/, #[cfg(test)] vs external mod, doctest disable, compile time reduction.
license: MIT
---

## What I do

Guide test organization in Rust/Cargo projects for maximum performance.

## How Cargo Tests Work

### Unit Tests

Located in `src/`. Cargo compiles with `--cfg test` and runs as single
executable.

### Integration Tests

Located in `tests/`. Each file is a separate binary that re-links the library.

- `src/` (unit): Single with lib, Private API
- `tests/`: Separate per file, Public API only

---

## Performance

### Compilation

Integration tests re-link library with each binary.

**Example**: Cargo consolidated integration tests → **3x faster compilation**,
**5x smaller disk**

### Runtime

- Tests run in parallel by default
- Cargo runs test binaries sequentially
- More binaries = longer critical path

**Example**: Consolidated several integration tests → **20s → 13s runtime**

---

## Organization Rules

### For Libraries (Published)

Use **one integration test** called `it`:

```
tests/
  it.rs

# Or for larger crates
tests/it/
  main.rs
  foo.rs
```

**Why**: Forces using public API like consumers do.

### For Internal Libraries

Avoid integration tests. Use unit tests in `tests.rs`:

```
src/
  lib.rs
  tests.rs       # Contains: #[cfg(test)] mod tests;
  tests/
    foo.rs       # With: mod tests;
```

**Benefits**: Access private API, faster compile, no re-linking.

---

## Tricks

### Disable Doc Tests

Doc tests are slow—each linked as separate binary.

```toml
[lib]
doctest = false
```

### External Test Module

```rust
// Instead of:
#[cfg(test)]
mod tests { /* tests here */ }

// Use:
#[cfg(test)]
mod tests;  // In tests.rs file
```

**Why**: Modifying tests won't recompile the library.

### Single Test Crate (Maximum Performance)

```toml
# In each crate
[lib]
test = false
```

Create one workspace test crate that links everything.

---

## Summary

1. **One integration test**: `tests/it.rs`
2. **Prefer unit tests**: Access private API, faster
3. **Disable doc tests**: In large internal projects
4. **External test module**: `mod tests;` in separate file
5. **Consider workspace test crate**: For maximum speed