liveBook · Manning

Chapter 11: Testing your Rust application

What you will learn in chapter 11 of Rust Web Development

This introduction explains why and where to test a Rust web service, distinguishes unit and integration tests, and introduces Rust’s basic testing tools.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction explains why and where to test a Rust web service, distinguishes unit and integration tests, and introduces Rust’s basic testing tools.

Key terms: business logic, critical workflow, side effects, codebase coverage, unit testing, integration testing, black box, Pagination, Error, Cargo

11.1 Unit testing our business logic

The section introduces self-contained tests as a way to check that business logic behaves as expected.

Key terms: self-contained tests, business logic

11.1.1 Testing the pagination logic and dealing with custom errors

This section develops unit tests for pagination behavior and shows how to compare custom errors when deriving PartialEq fails.

Key terms: extract_pagination, Pagination, MissingParameters, ParseError, limit, offset, unit tests, test module, assert_eq!, PartialEq

11.1.2 Testing the Config module with environment variables

This section tests configuration loading and resolves interference between tests that modify environment variables.

Key terms: dotenv, Config::new, .env, env::var, catch_unwind, assert!(result.is_err()), API key, parallel tests, environment variables, set_api_key

11.1.3 Testing the profanity module with a newly created mock server

This section tests the profanity checker using a configurable local mock server with predefined responses and a oneshot channel for graceful shutdown.

Key terms: check_profanity, Reqwest, mock server, predefined responses, route, query parameter, raw body, bytes, bind_with_graceful_shutdown, oneshot channel

11.2 Testing our Warp filters

This section tests a Warp authentication filter without a server and traces intermittent failures to token timing and shared environment-variable changes.

Key terms: authentication filter, Authorization header, warp::test, auth middleware, set_not_before, Utc::now, Session, nbf, Serde, config tests

11.3 Creating an integration testing setup

Defines the book’s integration-testing scope and presents environment options for testing the web service.

Key terms: integration testing, local database, mocking, external API endpoints, Docker environment, local setup, Cargo project, integration tests

11.3.1 Splitting up the codebase into a lib.rs and a binary

This section restructures the application into a reusable library and an executable server, preparing it for integration testing.

Key terms: lib.rs, binary, public functions, integration-test project, server.rs, dotenv, config, setup_store, run, rust_web_dev

11.3.2 Creating the integration-test crate and the oneshot server implementation

This section prepares a separate integration-test crate and a controllable web server for integration testing.

Key terms: oneshot channel, OneshotHandler, sender, integration-tests crate, Cargo.toml, rust-web-dev library, config module, handle_errors module, pub, .env file

11.3.3 Adding the registration test

This section builds a registration integration test and resolves its JSON-response and test-database problems.

Key terms: oneshot, register_new_user, Reqwest, assert_eq!, warp::reply::json(), JSON, test database, .env, Command builder, cargo run

11.3.4 Unwinding in case of an error

This section explains how the integration-test application catches unwinding panics and cleans up after a test failure.

Key terms: integration-tests, mock server, register_new_user, unwinding, AssertUnwindSafe, catch_unwind, futures_util, future, panic

11.3.5 Testing the login and posting questions

The section completes the route-testing example, suggests negative tests, and reflects on code improvements made during testing.

Key terms: registration, login, assert macro, error code, store, routes, JSON

11.4 Summary

Concepts explained in this chapter

← Chapter 10: Deploy your application

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.