← 返回命令列表

Linux command

cargo-test 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Run all tests

cargo test

Run specific test

cargo test [test_name]

Run tests in module

cargo test [module_name]::**

Run ignored tests

cargo test -- --ignored

Show output from passing tests

cargo test -- --show-output

Run documentation tests only

cargo test --doc

Run tests without stopping on failure

cargo test -- --no-fail-fast

Compile without running

cargo test --no-run

说明

cargo test compiles and runs the test suite for a Rust project. It discovers unit tests (functions annotated with `#test` in `src/`), integration tests (files in `tests/`), documentation tests (code blocks in doc comments), and benchmark tests. A test name filter can be provided to run only matching tests. Arguments after `--` are passed directly to the test harness rather than to Cargo. This allows controlling test execution behavior such as thread count, output capture, and whether ignored tests should run. Tests run in parallel by default; use `--test-threads=1` for sequential execution when tests have shared state.

参数

--no-run
Compile without running
--no-fail-fast
Run all tests regardless of failures
--doc
Run documentation tests only
--lib
Test library only
--bins
Test all binaries
--tests
Test all test targets
--benches
Test all benchmarks
--all-targets
Test all targets
-p, --package _spec_
Test specified packages
--workspace
Test all workspace members
-j, --jobs _n_
Parallel build jobs
--release
Test with release profile
--features _features_
Enable features
--all-features
Enable all features

FAQ

What is the cargo-test command used for?

cargo test compiles and runs the test suite for a Rust project. It discovers unit tests (functions annotated with `#test` in `src/`), integration tests (files in `tests/`), documentation tests (code blocks in doc comments), and benchmark tests. A test name filter can be provided to run only matching tests. Arguments after `--` are passed directly to the test harness rather than to Cargo. This allows controlling test execution behavior such as thread count, output capture, and whether ignored tests should run. Tests run in parallel by default; use `--test-threads=1` for sequential execution when tests have shared state.

How do I run a basic cargo-test example?

Run `cargo test` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does --no-run do in cargo-test?

Compile without running