Day 17: A Comprehensive Guide to Writing Tests in Rust.

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
Search for a command to run...

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
No comments yet. Be the first to comment.
Move beyond traditional RESTful thinking. Learn how to design APIs specifically for MCP (Model Context Protocol) servers. This guide covers the shift in mindset, a practical OpenAPI 3.1 example, and a Spring Boot implementation to make your services ...

Extending Kestra to Every Corner of Your Data Stack. Introduction: The Power of Plugins Imagine you're a master chef. You don't just have one knife - you have specialized tools for every task: a paring knife for delicate work, a chef's knife for chop...
Mastering Complex Orchestration Scenarios. Introduction: The Orchestrator's Toolkit Imagine you're conducting a symphony. You don't just wave your baton - you cue sections, adjust tempo, handle surprises, and ensure harmony. That's what advanced work...
From Data Extraction to Loading - A Practical Guide Introduction: Why ETL Still Matters in the Modern Data Stack Remember when data engineering was "extract, transform, load"? Some say ETL is dead, replaced by ELT, reverse ETL, and data mesh. But her...
Building Blocks of Declarative Orchestration. Introduction: The Power of Simplicity Imagine trying to build a house without understanding bricks, beams, and blueprints. That's what using an orchestration tool without understanding its core concepts f...
Testing is an integral part of software development, ensuring code reliability and functionality. Rust provides robust support for writing tests through its built-in testing framework. In this article, we'll explore the tools and conventions available in Rust for writing tests to ensure code correctness and maintainability.
Rust's testing framework is integrated into the language itself, making it easy to write and run tests. The convention is to create a tests module within the file to contain the test functions.
#[cfg(test)]
mod tests {
#[test]
fn test_function_name() {
// Test assertions go here
assert_eq!(2 + 2, 4);
}
}
#[cfg(test)]: Specifies that this code should only be compiled during tests.
mod tests: Defines the module for tests.
#[test]: Indicates a test function.
assert_eq!: Macro to assert the equality of two values.
Test functions are regular Rust functions annotated with #[test]. They should contain assertions that validate the expected behavior of the code under test. Rust provides several assertion macros like assert!, assert_eq!, and assert_ne! for different comparison scenarios.
fn add(a: i32, b: i32) -> i32 {
a + b
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_addition() {
assert_eq!(add(2, 3), 5);
assert_eq!(add(-5, 5), 0);
assert_eq!(add(100, 200), 300);
}
}
Rust's test runner executes all functions annotated with #[test]. Tests can be run using the cargo test command:
$ cargo test
cargo test automatically finds and runs test functions in the project.Rust supports organizing tests into separate modules, files, or even directories. Each test module or file can contain multiple test functions for different components or functionalities.
// In a separate test file or module
#[cfg(test)]
mod test_module {
#[test]
fn test_component_one() {
// Test assertions
}
#[test]
fn test_component_two() {
// Test assertions
}
}
Rust also supports features like doc-tests, enabling tests embedded in documentation comments (///).
/// This function adds two numbers.
///
/// # Examples
///
///
/// let result = add(2, 3);
/// assert_eq!(result, 5);
/// fn add(a: i32, b: i32) -> i32 {
a + b
}
Rust's testing framework offers a powerful and straightforward way to write tests, ensuring code correctness and providing confidence in software functionality. By leveraging Rust's testing conventions, assertion macros, and organizational practices, developers can create comprehensive test suites, promoting code reliability and maintainability. Embrace Rust's testing capabilities to build robust and resilient applications.
Happy coding with Rust!
I hope this helps, you!!
More such articles:
https://www.youtube.com/@maheshwarligade
\==========================**=========================
If this article adds any value to you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.