| created | "2025-12-16T00:00:00.000Z" |
| modified | "2025-12-16T00:00:00.000Z" |
| reviewed | "2025-12-16T00:00:00.000Z" |
| name | cargo-nextest |
| description | Next-generation test runner for Rust with parallel execution, advanced filtering, and CI integration.
Use when running tests, configuring test execution, setting up CI pipelines, or optimizing test performance.
Trigger terms: nextest, test runner, parallel tests, test filtering, test performance, flaky tests, CI testing.
|
cargo-nextest - Next-Generation Test Runner
cargo-nextest is a faster, more reliable test runner for Rust that executes each test in its own process for better isolation and parallel performance.
Installation
cargo install cargo-nextest --locked
cargo nextest --version
Basic Usage
cargo nextest run
cargo nextest run test_name
cargo nextest run -p package_name
cargo nextest run --verbose
cargo nextest run -- --ignored
cargo nextest run -- --include-ignored
Configuration File
Create .config/nextest.toml in your project root:
[profile.default]
retries = 0
test-threads = 8
fail-fast = false
success-output = "never"
failure-output = "immediate"
[profile.ci]
retries = 2
fail-fast = true
success-output = "never"
failure-output = "immediate-final"
slow-timeout = { period = "60s", terminate-after = 2 }
[profile.ci.junit]
path = "target/nextest/ci/junit.xml"
Test Filtering with Expression Language
cargo nextest run -E 'test(auth)'
cargo nextest run -E 'binary(my_app)'
cargo nextest run -E 'package(my_crate)'
cargo nextest run -E 'test(auth) and not test(slow)'
cargo nextest run -E 'kind(test)'
cargo nextest run -E 'kind(lib) and test(/)'
Expression Operators
test(pattern) - Match test name (regex)
binary(pattern) - Match binary name
package(pattern) - Match package name
kind(type) - Match test kind (lib, bin, test, bench, example)
platform(os) - Match target platform
not expr - Logical NOT
expr and expr - Logical AND
expr or expr - Logical OR
Parallel Execution
Nextest runs each test in its own process by default, providing:
- Better isolation - Tests cannot interfere with each other
- True parallelism - No global state conflicts
- Fault isolation - One test crash doesn't affect others
- Better resource management - Each test has clean environment
cargo nextest run --test-threads 4
cargo nextest run --test-threads 1
cargo nextest run --test-threads 0
Output Formats
cargo nextest run
cargo nextest run --message-format json
cargo nextest run --message-format json-pretty
cargo nextest run --profile ci
cargo llvm-cov nextest --html
Flaky Test Management
[profile.default]
retries = 3
[profile.default.overrides]
filter = 'test(flaky_)'
retries = 5
cargo nextest run --retries 3
cargo nextest run --retries 3 --failure-output immediate-final
GitHub Actions Integration
name: Test
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: Install nextest
uses: taiki-e/install-action@v2
with:
tool: nextest
- name: Run tests
run: cargo nextest run --profile ci --all-features
- name: Upload test results
if: always()
uses: actions/upload-artifact@v4
with:
name: test-results
path: target/nextest/ci/junit.xml
- name: Publish test results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
files: target/nextest/ci/junit.xml
Advanced Configuration
[profile.default]
slow-timeout = { period = "30s", terminate-after = 3 }
[test-groups.database]
max-threads = 1
[profile.default.overrides]
filter = 'test(db_)'
test-group = 'database'
[[profile.default.overrides]]
platform = 'x86_64-pc-windows-msvc'
slow-timeout = { period = "60s" }
Doctests Limitation
Important: nextest does not support doctests. Use cargo test for doctests:
cargo test --doc
cargo nextest run && cargo test --doc
Workspace Configuration
[profile.default]
default-filter = 'all()'
[[profile.default.overrides]]
filter = 'package(slow_tests)'
test-threads = 1
slow-timeout = { period = "120s" }
Comparison with cargo test
| Feature | cargo test | cargo nextest |
|---|
| Execution model | In-process | Per-test process |
| Parallelism | Thread-based | Process-based |
| Test isolation | Shared state | Complete isolation |
| Output format | Limited | Rich (JSON, JUnit) |
| Flaky test handling | Manual | Built-in retries |
| Doctests | Supported | Not supported |
| Performance | Good | Excellent |
Best Practices
-
Use profiles for different environments:
default for local development
ci for continuous integration
coverage for coverage analysis
-
Configure flaky test detection:
[profile.default]
retries = 2
-
Group resource-intensive tests:
[test-groups.expensive]
max-threads = 2
-
Set appropriate timeouts:
[profile.default]
slow-timeout = { period = "60s", terminate-after = 2 }
-
Use expression filters effectively:
cargo nextest run -E 'not test(slow_)'
-
Always run doctests separately in CI:
- run: cargo nextest run --all-features
- run: cargo test --doc --all-features
Troubleshooting
Tests timeout too quickly:
[profile.default]
slow-timeout = { period = "120s", terminate-after = 3 }
Too much parallel execution:
cargo nextest run --test-threads 4
Need test output for debugging:
cargo nextest run --success-output immediate --failure-output immediate
Flaky tests in CI:
[profile.ci]
retries = 3
failure-output = "immediate-final"
References