0Pricing
Learn Rust Coding · Lesson

Integration Tests

The tests directory.

Integration Tests is a free Learn Rust Coding lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.rs
  • tests/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 test

Running One File

Use --test with the file name (without extension) to run a single integration test file.

cargo test --test add

Shared 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 name runs one file
  • Shared helpers go in tests/common/mod.rs

Frequently asked questions

Is the “Integration Tests” lesson free?

Yes — the full text of “Integration Tests” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “Integration Tests”?

The tests directory. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Integration Tests” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Unit Tests
  2. Integration Tests
  3. Doc Comments
  4. Doc Tests
← Back to Learn Rust Coding