O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 3 - Unit Testing and Benchmarking

Unit tests are a neat way to increase and maintain code quality. Rust supports basic unit testing in its core package. Test functions are annotated with #[test]. Two macros, assert! and assert_eq!, can be used to declare the expected function results. The #[should_panic] annotation can be used to define that a test should fail with a panic. Unit tests are placed in the same file as the code they test. The test code can be separated from the actual code by putting it in a separate module annotated with #[cfg_test].

Integration tests go into a separate tests/directory in a Rust project. These are meant for testing larger portions of code. Unlike unit tests, integration tests run as if they were consumers ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required