← Back to list

rust-backend-testing
by caioniehues
My dotfiles - Claude Code configuration
⭐ 0🍴 0📅 Dec 23, 2025
SKILL.md
name: rust-backend-testing description: Guides Rust backend test development with testcontainers, mockall, and axum-test. Use when writing integration tests, mocking dependencies, testing HTTP handlers, or setting up test infrastructure for Axum/SQLx projects.
<essential_principles>
- Test at the Right Level - Unit (mockall), Integration (testcontainers), API (axum-test)
- Trait-Based Design - Define services behind traits for mockability
- Real Dependencies Over Mocks - Prefer testcontainers for database tests
- Test Isolation - Each test independent, use transactions or fresh containers </essential_principles>
use testcontainers::{runners::AsyncRunner, GenericImage, ImageExt};
use testcontainers::core::{IntoContainerPort, WaitFor};
async fn setup_postgres() -> Result<(impl std::any::Any, PgPool), Box<dyn std::error::Error>> {
let container = GenericImage::new("postgres", "15-alpine")
.with_exposed_port(5432.tcp())
.with_env_var("POSTGRES_PASSWORD", "test")
.with_env_var("POSTGRES_DB", "testdb")
.with_wait_for(WaitFor::message_on_stderr("ready to accept connections"))
.start()
.await?;
let port = container.get_host_port_ipv4(5432).await?;
let url = format!("postgres://postgres:test@localhost:{}/testdb", port);
let pool = PgPool::connect(&url).await?;
sqlx::migrate!("./migrations").run(&pool).await?;
Ok((container, pool))
}
use mockall::{automock, predicate::*};
#[automock]
pub trait UserRepository: Send + Sync {
async fn find_by_id(&self, id: i64) -> Result<Option<User>, DbError>;
}
#[tokio::test]
async fn test_user_service() {
let mut mock = MockUserRepository::new();
mock.expect_find_by_id()
.with(eq(1))
.returning(|_| Ok(Some(User { id: 1, name: "Test".into() })));
let service = UserService::new(Arc::new(mock));
let user = service.get_user(1).await.unwrap();
assert_eq!(user.name, "Test");
}
use axum_test::TestServer;
#[tokio::test]
async fn test_get_user() {
let app = create_router(state);
let server = TestServer::new(app).unwrap();
let response = server.get("/users/1").await;
response.assert_status_ok();
let user: User = response.json();
assert_eq!(user.name, "Test");
}
<success_criteria>
- Unit tests cover business logic with mocked dependencies
- Integration tests verify database operations with real PostgreSQL
- API tests confirm handler behavior end-to-end
- Tests are isolated and can run in any order
- cargo test passes with no flaky tests </success_criteria>
Score
Total Score
60/100
Based on repository quality metrics
✓SKILL.md
SKILL.mdファイルが含まれている
+20
✓LICENSE
ライセンスが設定されている
+10
○説明文
100文字以上の説明がある
0/10
○人気
GitHub Stars 100以上
0/15
○最近の活動
3ヶ月以内に更新がある
0/10
○フォーク
10回以上フォークされている
0/5
✓Issue管理
オープンIssueが50未満
+5
✓言語
プログラミング言語が設定されている
+5
○タグ
1つ以上のタグが設定されている
0/5
Reviews
💬
Reviews coming soon