Pruebas de integración
El directorio tests
Pruebas de integración es una lección gratuita de Learn Rust Coding en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Learn Rust Coding, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Learn Rust Coding incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
What Are Integration Tests?
Integration tests verify your crate's public API the way an external user would. They live outside src and can only call public items.
The tests Directory
Cargo treats every file in the top-level tests/ directory as a separate test crate. No #[cfg(test)] module is needed there.
tests/api.rstests/parsing.rs
A Library to Test
Integration tests target a library crate. Suppose src/lib.rs exposes a public function.
// src/lib.rs
pub fn add(a: i32, b: i32) -> i32 {
a + b
}Writing an Integration Test
In tests/add.rs, import the crate by name and test its public API. Each function still uses #[test].
// tests/add.rs
use my_lib::add;
#[test]
fn adds_two_numbers() {
assert_eq!(add(2, 2), 4);
}Public Only
Because integration tests are a separate crate, they can use only public (pub) items. Private functions are invisible — just like to any real consumer.
Running Integration Tests
cargo test runs unit and integration tests together. Each file under tests/ appears as its own test binary in the output.
cargo testRunning One File
Use --test with the file name (without extension) to run a single integration test file.
cargo test --test addShared Test Helpers
To share setup code without making it a test file, put helpers in tests/common/mod.rs. Files in subdirectories are not treated as test crates.
// tests/common/mod.rs
pub fn setup() {
// shared setup logic
}Using the Helper
Declare the module and call its functions from your integration test.
// tests/api.rs
mod common;
#[test]
fn uses_setup() {
common::setup();
assert!(true);
}Unit vs Integration
Two complementary test styles:
- Unit tests live in
src, can test private items, fast and focused - Integration tests live in
tests/, test the public API end to end
Testing a Binary Crate
Integration tests target libraries, not binaries. A common pattern is to keep logic in src/lib.rs and have a thin src/main.rs that calls it, so both unit and integration tests can reach the code.
// src/main.rs
fn main() {
println!("{}", my_lib::add(2, 3));
}Quick Check
Where do integration tests live, and what can they access?
Recap
You learned integration testing:
- Files in
tests/are separate test crates - They import your crate by name and test only public items
cargo test --test nameruns one file- Shared helpers go in
tests/common/mod.rs
Preguntas frecuentes
¿La lección «Pruebas de integración» es gratis?
Sí — el texto completo de «Pruebas de integración» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Learn Rust Coding, actualiza a CoddyKit PRO. El curso de Learn Rust Coding incluye 4 lecciones en total.
¿Qué aprenderé en «Pruebas de integración»?
El directorio tests Practicas Learn Rust Coding con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Learn Rust Coding?
No se requiere experiencia previa. Learn Rust Coding en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.
¿Cuánto tiempo toma la lección «Pruebas de integración»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Learn Rust Coding?
Sí. Cada lección de Learn Rust Coding incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Pruebas unitarias
- Pruebas de integración
- Comentarios de documentación
- Pruebas de documentación