Rust Integration Testing Fundamentals

RustRustBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, integration testing is discussed, which involves testing multiple parts of a library together using its public interface. Integration tests can be placed in the tests directory next to the src directory in a Rust crate, and are executed using the cargo test command. Additionally, common code can be shared between integration tests by creating a module with public functions and importing it within the tests.

Note: If the lab does not specify a file name, you can use any file name you want. For example, you can use main.rs, compile and run it with rustc main.rs && ./main.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL rust(("`Rust`")) -.-> rust/DataTypesGroup(["`Data Types`"]) rust(("`Rust`")) -.-> rust/FunctionsandClosuresGroup(["`Functions and Closures`"]) rust(("`Rust`")) -.-> rust/DataStructuresandEnumsGroup(["`Data Structures and Enums`"]) rust(("`Rust`")) -.-> rust/AdvancedTopicsGroup(["`Advanced Topics`"]) rust(("`Rust`")) -.-> rust/ProjectManagementandOrganizationGroup(["`Project Management and Organization`"]) rust/DataTypesGroup -.-> rust/integer_types("`Integer Types`") rust/FunctionsandClosuresGroup -.-> rust/function_syntax("`Function Syntax`") rust/FunctionsandClosuresGroup -.-> rust/expressions_statements("`Expressions and Statements`") rust/DataStructuresandEnumsGroup -.-> rust/method_syntax("`Method Syntax`") rust/AdvancedTopicsGroup -.-> rust/operator_overloading("`Traits for Operator Overloading`") rust/ProjectManagementandOrganizationGroup -.-> rust/module_system("`Module System`") subgraph Lab Skills rust/integer_types -.-> lab-99283{{"`Rust Integration Testing Fundamentals`"}} rust/function_syntax -.-> lab-99283{{"`Rust Integration Testing Fundamentals`"}} rust/expressions_statements -.-> lab-99283{{"`Rust Integration Testing Fundamentals`"}} rust/method_syntax -.-> lab-99283{{"`Rust Integration Testing Fundamentals`"}} rust/operator_overloading -.-> lab-99283{{"`Rust Integration Testing Fundamentals`"}} rust/module_system -.-> lab-99283{{"`Rust Integration Testing Fundamentals`"}} end

Integration testing

Unit tests are testing one module in isolation at a time: they're small and can test private code. Integration tests are external to your crate and use only its public interface in the same way any other code would. Their purpose is to test that many parts of your library work correctly together.

Cargo looks for integration tests in tests directory next to src.

File src/lib.rs:

// Define this in a crate called `adder`.
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

File with test: tests/integration_test.rs:

#[test]
fn test_add() {
    assert_eq!(adder::add(3, 2), 5);
}

Running tests with cargo test command:

$ cargo test
running 0 tests

test result: ok. 0 passed
0 failed
0 ignored
0 measured
0 filtered out

Running target/debug/deps/integration_test-bcd60824f5fbfe19

running 1 test
test test_add ... ok

test result: ok. 1 passed
0 failed
0 ignored
0 measured
0 filtered out

Doc-tests adder

running 0 tests

test result: ok. 0 passed
0 failed
0 ignored
0 measured
0 filtered out

Each Rust source file in the tests directory is compiled as a separate crate. In order to share some code between integration tests we can make a module with public functions, importing and using it within tests.

File tests/common/mod.rs:

pub fn setup() {
    // some setup code, like creating required files/directories, starting
    // servers, etc.
}

File with test: tests/integration_test.rs

// importing common module.
mod common;

#[test]
fn test_add() {
    // using common code.
    common::setup();
    assert_eq!(adder::add(3, 2), 5);
}

Creating the module as tests/common.rs also works, but is not recommended because the test runner will treat the file as a test crate and try to run tests inside it.

Summary

Congratulations! You have completed the Integration Testing lab. You can practice more labs in LabEx to improve your skills.

Other Rust Tutorials you may like