CUSTOMISED
Expert-led training for your team
Dismiss
Rust Testing: How to Write and Run Tests in Rust

6 April 2023

Rust Testing: How to Write and Run Tests in Rust

This article is brought to you by JBI Training, the UK's leading technology training provider.   Learn more about JBI's Power BI training courses including Rust and Power BI - Beyond the Basics

Introduction Testing is an essential part of software development, and Rust provides a built-in testing framework to make it easy to write and run tests for your Rust code. Writing tests can help you catch bugs and ensure that your code behaves as expected, making it more reliable and maintainable.

In this article, we'll explore how to write and run tests in Rust. We'll cover the basics of Rust testing and how to use the built-in testing framework. We'll also look at some best practices for writing effective tests and show you how to use some popular testing libraries in Rust.

Getting Started with Rust Testing Rust's testing framework is built into the standard library, which means that you don't need to install any additional dependencies to start writing tests. To get started, create a new Rust project by running the following command:

$ cargo new myproject

This will create a new Rust project with a basic directory structure. Inside the src directory, you'll find a file named main.rs. This is the entry point for your Rust application.

To create a test module, create a new file in the src directory called lib.rs. In this file, define a new module for your tests:

#[cfg(test)] mod tests { // tests go here }

The #[cfg(test)] attribute tells the Rust compiler to only compile this module when running tests. Inside this module, you can define one or more test functions. A test function is a regular Rust function with the #[test] attribute:

#[test] fn test_addition() { assert_eq!(2 + 2, 4); }

In this example, we've defined a test function called test_addition that uses the assert_eq! macro to check that the result of 2 + 2 is equal to 4.

To run your tests, use the following command:

$ cargo test

This will compile and run all the tests in your Rust project. If everything passes, you'll see output like the following:

running 1 test test tests::test_addition ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Congratulations, you've just written and run your first Rust test!

Writing Effective Tests Now that you know how to write basic tests in Rust, let's look at some best practices for writing effective tests.

First, make sure that your tests are isolated from each other. Each test should operate on its own data and not rely on any external state. This will ensure that your tests are independent and can be run in any order.

Second, make sure that your tests are easy to read and understand. Use descriptive names for your test functions and provide clear comments and explanations for any complex logic.

Third, use assertions to test that your code behaves as expected. Rust provides a wide range of assertion macros, such as assert_eq!, assert_ne!, and assert!(expression). Use these macros to test that your code produces the expected output and handles edge cases correctly.

Using Testing Libraries Rust provides a number of popular testing libraries that can make it even easier to write tests for your Rust code. Here are a few of the most popular:

  • mockito: A library for mocking HTTP servers in Rust tests.
  • quickcheck: A library for property-based testing in Rust.
  • assert_cmd: A library for testing command-line applications in Rust.

To use one of these libraries in your Rust project, add the library to your `Cargo.tom

Writing Integration Tests

Integration tests are used to test how the different parts of your program interact with each other. Rust provides a convenient way to write integration tests by putting them in a tests directory at the root of your project. Each file in the directory is compiled as a separate crate and linked to the main crate.

Let's create an integration test for our add function. In your project directory, create a tests directory and a file called integration_test.rs inside it. Add the following code to the file:

#[cfg(test)] mod tests { use super::add; #[test] fn test_add() { assert_eq!(add(2, 3), 5); } }

This test module is almost identical to the one in unit_test.rs. The difference is that we import the add function from the lib module instead of defining it here. The #[cfg(test)] attribute makes sure that the module is only compiled when running tests.

To run the integration test, use the following command:

cargo test --test integration_test

This command runs all tests in the integration_test.rs file. You can also use the --test-threads flag to specify the number of threads to use when running tests in parallel. For example, to run the tests in two threads, use the following command:

cargo test --test integration_test -- --test-threads=2

Conclusion

In this guide, we covered the basics of testing in Rust. We learned how to write unit tests and integration tests, and how to run them using cargo. Writing tests can help you write more reliable and maintainable code, and also helps catch bugs early in the development process. By following the principles of test-driven development, you can ensure that your code is correct and robust, and avoid the headache of debugging later on.

About the author: Daniel West
Tech Blogger & Researcher for JBI Training

CONTACT
+44 (0)20 8446 7555

[email protected]

SHARE

 

Copyright © 2023 JBI Training. All Rights Reserved.
JB International Training Ltd  -  Company Registration Number: 08458005
Registered Address: Wohl Enterprise Hub, 2B Redbourne Avenue, London, N3 2BS

Modern Slavery Statement & Corporate Policies | Terms & Conditions | Contact Us

POPULAR

Rust training course                                                                          React training course

Threat modelling training course   Python for data analysts training course

Power BI training course                                   Machine Learning training course

Spring Boot Microservices training course              Terraform training course

Kubernetes training course                                                            C++ training course

Power Automate training course                               Clean Code training course