liveBook · Manning

Chapter 14: Testing and building your code from tests

What you will learn in chapter 14 of Learn Rust in a Month of Lunches

The introduction explains why growing Rust programs need structure and tests, then outlines the test-driven development cycle.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why growing Rust programs need structure and tests, then outlines the test-driven development cycle.

Key terms: structure, testing, logic errors, compiler, test-driven development, TDD

14.1 Crates and modules

Introduces crates as the files that make up Rust code and modules as a way to organize names and control access as code grows.

Key terms: crate, module, mod, namespace, std::collections::HashMap, private, public, pub, encapsulation

14.1.1 Module basics

Shows how to declare a Rust module, why an import must be inside it, and how to make one of its functions callable from main.

Key terms: mod, module, use std::fmt::Display;, print_things, pub, fn, pub fn, private, prints_one_thing, main()

14.1.2 More on how the pub keyword works

This section explains how `pub` affects the visibility of structs, enums, traits, and modules differently.

Key terms: pub, struct, tuple structs, parameters, enum, variant, trait, method, module, crate

14.1.3 Modules inside modules

This section explains how nested modules access their parents, how module paths work, and how declaring a separate file makes Rust include it in a project.

Key terms: child module, parent module, pub, crate::, super::, module, main.rs, lib.rs, functions.rs, mod functions;

14.2 Testing

This section introduces Rust testing by explaining that tests can live alongside the code they test rather than in separate files.

Key terms: tests, separate test files

14.2.1 Just add #[test], and now it’s a test

This section shows how to mark and run Rust tests, what test functions require, and how Rust decides whether a test passes.

Key terms: #[test], Test, cargo test, main(), test functions, arguments, panic, assert_eq!

14.2.2 What happens when tests fail

This section shows what failed assertions report and how to get more detail from a backtrace when the failure is hard to locate.

Key terms: assert_eq!, assert!, panic, left, right, RUST_BACKTRACE, environment variables, backtrace, std::env::set_var(), verbose backtrace

14.2.3 Writing multiple tests

The section shows that Rust can run multiple tests and explains how to organize them in a test-only module.

Key terms: #[test], test functions, mod, #[cfg(test)], use super::*;

14.3 Test-driven development

Introduces test-driven development as writing failing tests before code, then keeping those tests to catch problems after later changes.

Key terms: test-driven development, TDD, tests

14.3.1 Building a calculator: Starting with the tests

The section starts a test-driven Subtractor by defining expected results, running the tests against a placeholder method, and adding an initial input check.

Key terms: TDD, Subtractor, math(), i32, test output, OKAY_CHARACTERS, .chars(), .any(), panic

14.3.2 Putting the calculator together

The section builds a tested Subtractor step by step, using failing tests to reveal parsing mistakes and guide fixes.

Key terms: Subtractor, total, num_to_parse, i32, ParseIntErrors, Operation, Add, Subtract, .do_operation(), TDD

14.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 13: Box and Rust documentation Chapter 15: Default, the builder pattern, and Deref →

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.