top of page

How to use pretty_assertions for Rust test cases

Writer's picture: Ruud WijnandsRuud Wijnands
pretty_assertions

Below, you’ll find a set of exercises designed to help you practice with the pretty_assertion crate in Rust. Each exercise focuses on a commonly used feature of `pretty_assertions` and follows the structure:


  1. Feature Explanation

  2. Working Example

  3. Exercise

  4. Solution (with detailed explanation)


Make sure you add `pretty_assertions` to your development dependencies in `Cargo.toml`:

Cargo.toml with pretty_assertions dependency

Import its macros in your test files:

pretty_assertions library import

Basic `assert_eq!` with Pretty Printed Differences

Feature Explanation

At its core, pretty_assertions replaces the standard assert_eq! and assert_ne! macros with a version that highlights differences in a much more straightforward, color-coded way. This is extremely helpful when you compare large strings or data structures. The usage is almost identical to Rust’s built-in `assert_eq!` and `assert_ne!`, but if the test fails, you get a pretty diff.


Example

In the following test, we intentionally have a failure scenario to show off the diff. We’ll see a better diff than the default if we compare two different strings.

If you run this, you’ll see a panic that includes colored highlights showing what’s different between "Rust!" and "World!".


Exercise - 1

Goal: Create a test that compares two _identical_ strings, e.g., "Hello, World!" vs. "Hello, World!", and ensures they are equal using assert_eq!. Name the test something like test_equal_strings. The test should pass, not panic.


Using `assert_ne!` and Checking for Non-Equality

Feature Explanation

assert_ne! from pretty_assertions works similarly to assert_eq! but checks that two values are _not_ equal. If they are equal, the test fails and shows the same kind of pretty diff.


Example

In the snippet below, we check that the two strings are unequal. If they were the same, we’d see a panic with a diff highlighting that no difference exists.


Exercise - 2

Goal: Write a test that uses assert_ne! to check that two integers 10 and 20 are not equal. Name the test test_numbers_not_equal. The test should pass if the integers differ.


Comparing Multiline Strings

Feature Explanation

One of the best features of pretty_assertions is how it highlights differences between large or multiline strings. Regular assert_eq! can be hard to read with multiline outputs. With pretty_assertions, you get line-by-line diffs, similar to a Git diff.


Example

In this example, we compare two multiline strings. One has an extra comma that causes them to differ. If the test fails, you’ll see exactly which line is different, with coloring (if your terminal supports it).


Exercise - 3

Goal: Create a new test named test_multiline_strings in which you compare two multiline strings that are _identical_. Use a string with at least three lines. The test should pass. This will illustrate that pretty_assertions can handle multiline strings without showing a diff if they match perfectly.


Comparing Complex Data Structures (e.g., Vectors or HashMaps)

Feature Explanation

pretty_assertions also prettifies diffs for complex data structures (like Vec, HashMap, custom structs that implement Debug, etc.). If you have large nested data, the differences are shown in a more structured, readable fashion.


Example

Below, we demonstrate comparing two vectors of integers. If they differ, you’ll see a clear, line-by-line breakdown of which items mismatch:

Exercise - 4

Goal: Write a test that compares two HashMap<u32, &str>. They should be identical. Name the test test_hashmap_equality, and verify that pretty_assertions can handle the comparison without error. Include at least two key-value pairs.

Tip: Don’t forget to use std::collections::HashMap at the top.

Custom Types that Implement Debug

Feature Explanation

pretty_assertions relies on types implementing the Debug trait to render differences. For your custom structs, as long as they implement Debug, the crate can show a diff if they differ.


Example

Here, we have a struct Point with x and y coordinates. If we compare two different instances, we’ll see a color-coded difference highlighting which fields differ:


Exercise - 5

Goal: Create a struct named User with fields id: u32 and name: String. Implement Debug (derive it) for the struct. Write a test test_user_struct_equality comparing two identical User instances. The test should pass.


Solutions

In this section, you can find the solutions to the exercises above.


Solution - 1

Explanation:

  • We import assert_eq from pretty_assertions.

  • We compare two identical strings.

  • Because they’re equal, the test passes, and you won’t see any diff output.

  • The point is to illustrate the usage of assert_eq from pretty_assertions in a test that succeeds.


Solution - 2

Explanation:

  • We’re verifying that 10 and 20 are unequal.

  • assert_ne!(num1, num2) will pass since the values differ.

  • If they were the same, we’d see a diff that highlights zero differences (which is ironically helpful if you made a mistake in your test logic).

Solution - 3

Explanation:

  • We define two multiline strings expected and actual.

  • Both strings are identical.

  • Because they match exactly, assert_eq! succeeds silently (no diff needed).


Solution - 4


Explanation:

  • We create two HashMap<u32, &str> with the same key-value pairs.

  • assert_eq!(map1, map2) passes without showing any diff.

  • If there was a difference, we’d see a pretty-printed structure comparison.


Solution - 5

Note: If you want to compare the User instances directly (not just their fields), also derive PartialEq so that Rust knows how to determine the equality of your struct:

Then you can do assert_eq!(user1, user2); directly.


Explanation:

  • We define struct User with derive(Debug) so it can be printed.

  • We create two identical instances, user1 and user2.

  • We compare their fields with assert_eq!.

  • Alternatively, if we derived PartialEq, we could compare the structs directly.


This exercise shows how pretty_assertions can help you debug differences in custom types, provided those types implement Debug (and optionally PartialEq if you want to compare them directly).


Wrap-Up

By working through these exercises, you’ve practiced many of the key features of pretty_assertions:


  • Replacing standard assert_eq! and assert_ne! macros to get prettier diffs.

  • Comparing everything from basic data (ints, strings) to complex data structures (vectors, hash maps) and custom types.

  • Observing how multiline strings and large data structures get clearer, color-coded highlights for any differences.


You can have a look at the code examples above on GitHub here: https://github.com/DigitalDevBlog/pretty_assertions_examples.git

Enjoy more explicit test failures—and debug faster!


 
 
 

Comments


Sign Up For Updates

Thanks for signing up!

  • YouTube
  • Twitter
bottom of page