6 Unit testing

 

This chapter covers

  • Understanding how unit testing is unique in Rust
  • Exploring unit testing dos and don’ts, features, and frameworks
  • Discussing unit testing with parallel code
  • Writing unit tests that consider refactoring
  • Exploring tools to help with refactoring
  • Measuring code covered by tests
  • Testing strategies for dealing with Rust’s ecosystem

Unit testing is one way to improve code quality, as it can catch regressions and ensure code meets requirements before shipping. Rust includes a built-in unit testing framework to make your life easier. In this chapter, we’ll review some of the features Rust provides and discuss some of the shortfalls of Rust’s unit testing framework—and how to overcome them.

6.1 How testing is different in Rust

Before we jump into the details of Rust’s unit testing features, we should talk about the differences between Rust and other languages and how they relate to unit testing. For those coming from languages like Haskell or Scala, you may find Rust has similar properties when it comes to testing. Compared to most languages, however, Rust varies greatly, in that the kinds of unit tests you might see in other languages aren’t necessary in Rust.

6.2 Review of built-in testing features

6.3 Testing frameworks

6.4 What not to test: Why the compiler knows better than you

6.5 Handling parallel test special cases and global state

6.6 Thinking about refactoring

6.7 Refactoring tools

6.7.1 Reformatting

6.7.2 Renaming

6.7.3 Relocating

6.7.4 Rewriting

6.8 Code coverage

6.9 Dealing with a changing ecosystem

Summary

sitemap