Testes de integração
O diretório de testes
Testes de integração é uma aula grátis de Learn Rust Coding no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Learn Rust Coding, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Learn Rust Coding inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em 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
Perguntas Frequentes
A aula “Testes de integração” é grátis?
Sim — o texto completo de “Testes de integração” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Learn Rust Coding, atualize para CoddyKit PRO. O curso de Learn Rust Coding inclui 4 aulas no total.
O que vou aprender em “Testes de integração”?
O diretório de testes Você pratica Learn Rust Coding com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Learn Rust Coding?
Nenhuma experiência prévia é necessária. Learn Rust Coding no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.
Quanto tempo leva a aula “Testes de integração”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Learn Rust Coding?
Sim. Cada aula de Learn Rust Coding inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Testes unitários
- Testes de integração
- Comentários de documentação
- Testes de documentação