liveBook · Manning

Integration test

As explained in Code Like a Pro in Rust

An integration test is a software test that compiles and executes code exercising a component through interfaces similar to those used by other components or external clients.

An integration test exercises a component through its public interface, as another component or user would. In Rust, integration tests live outside the main source tree and compile as separate crates, so they cannot call private helpers. Use them alongside unit tests, which can check those helpers directly. section 7.1

A practical Rust workflow:

  • Put a test file in the top-level `tests/` directory. Import the crate’s public API, mark test functions with `#[test]`, and check results with assertions. Cargo discovers and runs the tests. section 7.1
  • Write cases that reflect how someone actually uses the API. A passing result checks behavior; awkward test setup can also reveal an interface that needs redesign. section 7.2
  • Keep focused unit tests for internal behavior. Do not duplicate their private-helper checks through an integration harness. section 7.1 section 7.2
Listing 7.2 Code sample of the integration test for quicksort implementation
use quicksort::quicksort;

#[test]
fn test_quicksort() {
   let mut values = vec![12, 1, 5, 0, 6, 2];
   quicksort(&mut values);
   assert_eq!(values, vec![0, 1, 2, 5, 6, 12]);

   let mut values = vec![1, 13, 5, 10, 6, 2, 0];
   quicksort(&mut values);
   assert_eq!(values, vec![0, 1, 2, 5, 6, 10, 13]);
}

A `tests/`-style test imports the public `quicksort` function, supplies two input vectors, and asserts the sorted outputs. section 7.1

Choose the harness to match the boundary. Rust’s built-in tests are sufficient for many cases and need no tooling beyond the Rust toolchain. For an HTTP service, curl or HTTPie may be simpler; a CLI may be easier to test with a Bash, Ruby, or Python script. Weigh that convenience against the portability of an all-Rust test suite. section 7.3 section 7.6

Property-based tests can broaden coverage: use `proptest` to generate inputs and compare results against an expected behavior. If generated cases write to a database or make network requests, restore a known state before and after tests so side effects do not contaminate later runs. Executable rustdoc examples offer another public-facing check: `cargo test` compiles and runs their code and assertions. section 7.4.2 section 2.8.1

How Code Like a Pro in Rust covers it

How deep it goes
Works through Cargo test placement, imports and assertions, then weighs harness choices and generated-test side effects; does not detail framework internals.
Examples
Uses a quicksort test over two vectors, generated vectors checked against sorting, and an asserted rustdoc example.
What it assumes
Assumes familiarity with Rust crates, imports and assertions while showing where integration tests live and how Cargo discovers them.
What you can do after
Apply it via Cargo to test a Rust crate’s exported behavior and choose an external harness when the interface warrants one.
How it connects
Connects integration testing to unit testing, public APIs, API design, property-based testing and executable documentation.

Also explained in

Compare how 31 books explain integration test →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.