codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🦀

Rust

41 / 58 topics
41Testing42Unit Tests43Integration Tests
Tutorials/Rust/Testing
🦀Rust

Testing

Updated 2026-05-15
10 min read

Testing

Introduction

Welcome to the section on testing in Rust! Writing robust tests is a crucial part of software development, ensuring that your code behaves as expected. Rust provides a powerful and flexible testing framework built into its standard library, making it easy to write and run tests.

In this tutorial, you'll learn how to write unit tests, integration tests, and even benchmarks using Rust's built-in tools. By the end of this section, you should be comfortable writing and running tests in your Rust projects.

Concept

Rust's testing framework is designed to be simple yet powerful. It supports three types of tests:

  1. Unit Tests: These are small, isolated tests that focus on a single function or module.
  2. Integration Tests: These tests check the interaction between different parts of your codebase.
  3. Benchmarks: These measure the performance of your code.

Writing Tests

Rust uses the #[test] attribute to mark functions as test cases. Here's a basic example:

Rust
1#[cfg(test)]
2mod tests {
3 #[test]
4 fn it_works() {
5 assert_eq!(2 + 2, 4);
6 }
7}

In this example:

  • The #[cfg(test)] attribute tells the Rust compiler to include the module only when running tests.
  • The #[test] attribute marks the function as a test case.

Running Tests

To run tests in your Rust project, use the following command:

Terminal
$ cargo test

This command will compile your code and execute all functions marked with #[test]. If any test fails, it will display detailed information about the failure.

Examples

Let's dive into some practical examples to illustrate how to write different types of tests in Rust.

Unit Tests

Here's an example of a unit test for a simple function:

Rust
1pub fn add(a: i32, b: i32) -> i32 {
2 a + b
3}
4
5#[cfg(test)]
6mod tests {
7 use super::*;
8
9 #[test]
10 fn test_add() {
11 assert_eq!(add(2, 3), 5);
12 assert_ne!(add(1, 1), 0);
13 }
14}

In this example:

  • The add function takes two integers and returns their sum.
  • The test_add function tests the add function using assertions.

Integration Tests

Integration tests are placed in a separate directory called tests/. Here's an example:

  1. Create a new file tests/integration_test.rs:
Rust
1use my_crate::add;
2
3#[test]
4fn test_add_integration() {
5 assert_eq!(add(5, 7), 12);
6}
  1. Run the integration tests using:
Terminal
$ cargo test

Benchmarks

Rust also supports benchmarking using the criterion crate. Here's how you can write a simple benchmark:

  1. Add criterion to your Cargo.toml:
toml
1[dev-dependencies]
2criterion = "0.3"
  1. Create a new file benches/bench.rs:
Rust
1use criterion::{black_box, Criterion};
2use my_crate::add;
3
4fn add_benchmark(c: &mut Criterion) {
5 c.bench_function("add", |b| b.iter(|| add(black_box(1), black_box(2))));
6}
7
8criterion_group!(benches, add_benchmark);
9criterion_main!(benches);
  1. Run the benchmarks using:
Terminal
$ cargo bench

What's Next?

In this section, you learned how to write and run unit tests in Rust. In the next section, we'll explore more advanced testing techniques, including mocking and property-based testing.

Stay tuned for more insights into building robust applications with Rust!


PreviousBuild SystemNext Unit Tests

Recommended Gear

Build SystemUnit Tests