重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
rust-testing by autumnsgrove/groveengine
npx skills add https://github.com/autumnsgrove/groveengine --skill rust-testing在以下情况下激活此技能:
# 运行所有测试
cargo test
# 显示输出
cargo test -- --nocapture
# 运行特定测试
cargo test test_user_create
# 运行模块内的测试
cargo test auth::
# 运行被忽略的测试
cargo test -- --ignored
# 仅运行文档测试
cargo test --doc
# 仅运行集成测试
cargo test --test integration
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add() {
assert_eq!(add(2, 3), 5);
}
#[test]
fn test_add_negative() {
assert_eq!(add(-1, -1), -2);
}
}
#[test]
fn regular_test() { }
#[test]
#[ignore]
fn slow_test() { } // 除非使用 --ignored,否则跳过
#[test]
#[should_panic]
fn test_panic() {
panic!("This should panic");
}
#[test]
#[should_panic(expected = "specific message")]
fn test_panic_message() {
panic!("specific message here");
}
#[test]
fn test_with_result() -> Result<(), String> {
let result = some_operation()?;
assert_eq!(result, expected);
Ok(())
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
// 基础断言
assert_eq!(1 + 1, 2);
assert_ne!(1 + 1, 3);
assert!(true);
// 带消息的断言
assert_eq!(result, expected, "values should match: got {}", result);
// 模式匹配
assert!(matches!(value, Pattern::Variant(_)));
// Option/Result 断言
assert!(some_option.is_some());
assert!(some_result.is_ok());
// tests/api_integration.rs
use my_crate::{Config, Server};
#[test]
fn test_server_startup() {
let config = Config::default();
let server = Server::new(config);
assert!(server.start().is_ok());
}
project/
├── Cargo.toml
├── src/
│ ├── lib.rs # 包含 #[cfg(test)] 的单元测试
│ └── user.rs # 包含内联测试的模块
└── tests/ # 集成测试
├── common/
│ └── mod.rs # 共享工具
└── api_test.rs
pub trait UserRepository {
fn find_by_id(&self, id: u64) -> Option<User>;
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
struct MockUserRepo {
users: HashMap<u64, User>,
}
impl UserRepository for MockUserRepo {
fn find_by_id(&self, id: u64) -> Option<User> {
self.users.get(&id).cloned()
}
}
#[test]
fn test_user_service() {
let mut users = HashMap::new();
users.insert(1, User { id: 1, email: "test@example.com".into() });
let repo = MockUserRepo { users };
let service = UserService::new(Box::new(repo));
let user = service.get_user(1).unwrap();
assert_eq!(user.email, "test@example.com");
}
}
#[tokio::test]
async fn test_async_operation() {
let result = fetch_data().await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_with_timeout() {
let result = tokio::time::timeout(
Duration::from_secs(5),
slow_operation()
).await;
assert!(result.is_ok());
}
/// 将两个数字相加。
///
/// # 示例
///
/// ```
/// use my_crate::add;
/// let result = add(2, 3);
/// assert_eq!(result, 5);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
use proptest::prelude::*;
proptest! {
#[test]
fn test_add_commutative(a: i32, b: i32) {
prop_assert_eq!(add(a, b), add(b, a));
}
}
# 使用 cargo-tarpaulin
cargo install cargo-tarpaulin
cargo tarpaulin --out Html
# 使用 cargo-llvm-cov
cargo install cargo-llvm-cov
cargo llvm-cov --html
查看 AgentUsage/testing_rust.md 获取完整文档,包括:
每周安装次数
54
代码仓库
GitHub 星标数
2
首次出现
2026年2月5日
安全审计
安装于
opencode54
gemini-cli54
codex54
github-copilot53
kimi-cli53
amp53
Activate this skill when:
# Run all tests
cargo test
# With output
cargo test -- --nocapture
# Run specific test
cargo test test_user_create
# Run tests in module
cargo test auth::
# Run ignored tests
cargo test -- --ignored
# Doc tests only
cargo test --doc
# Integration tests only
cargo test --test integration
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add() {
assert_eq!(add(2, 3), 5);
}
#[test]
fn test_add_negative() {
assert_eq!(add(-1, -1), -2);
}
}
#[test]
fn regular_test() { }
#[test]
#[ignore]
fn slow_test() { } // Skip unless --ignored
#[test]
#[should_panic]
fn test_panic() {
panic!("This should panic");
}
#[test]
#[should_panic(expected = "specific message")]
fn test_panic_message() {
panic!("specific message here");
}
#[test]
fn test_with_result() -> Result<(), String> {
let result = some_operation()?;
assert_eq!(result, expected);
Ok(())
}
// Basic
assert_eq!(1 + 1, 2);
assert_ne!(1 + 1, 3);
assert!(true);
// With messages
assert_eq!(result, expected, "values should match: got {}", result);
// Pattern matching
assert!(matches!(value, Pattern::Variant(_)));
// Option/Result
assert!(some_option.is_some());
assert!(some_result.is_ok());
// tests/api_integration.rs
use my_crate::{Config, Server};
#[test]
fn test_server_startup() {
let config = Config::default();
let server = Server::new(config);
assert!(server.start().is_ok());
}
project/
├── Cargo.toml
├── src/
│ ├── lib.rs # Unit tests in #[cfg(test)]
│ └── user.rs # Module with inline tests
└── tests/ # Integration tests
├── common/
│ └── mod.rs # Shared utilities
└── api_test.rs
pub trait UserRepository {
fn find_by_id(&self, id: u64) -> Option<User>;
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
struct MockUserRepo {
users: HashMap<u64, User>,
}
impl UserRepository for MockUserRepo {
fn find_by_id(&self, id: u64) -> Option<User> {
self.users.get(&id).cloned()
}
}
#[test]
fn test_user_service() {
let mut users = HashMap::new();
users.insert(1, User { id: 1, email: "test@example.com".into() });
let repo = MockUserRepo { users };
let service = UserService::new(Box::new(repo));
let user = service.get_user(1).unwrap();
assert_eq!(user.email, "test@example.com");
}
}
#[tokio::test]
async fn test_async_operation() {
let result = fetch_data().await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_with_timeout() {
let result = tokio::time::timeout(
Duration::from_secs(5),
slow_operation()
).await;
assert!(result.is_ok());
}
/// Adds two numbers together.
///
/// # Examples
///
/// ```
/// use my_crate::add;
/// let result = add(2, 3);
/// assert_eq!(result, 5);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
use proptest::prelude::*;
proptest! {
#[test]
fn test_add_commutative(a: i32, b: i32) {
prop_assert_eq!(add(a, b), add(b, a));
}
}
# Using cargo-tarpaulin
cargo install cargo-tarpaulin
cargo tarpaulin --out Html
# Using cargo-llvm-cov
cargo install cargo-llvm-cov
cargo llvm-cov --html
See AgentUsage/testing_rust.md for complete documentation including:
Weekly Installs
54
Repository
GitHub Stars
2
First Seen
Feb 5, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode54
gemini-cli54
codex54
github-copilot53
kimi-cli53
amp53
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
122,000 周安装