Different ways to write unit test-cases in Rust

Loggers in Rust
Reading Time: 2 minutes

As a developer, we know that how important is to write unit test-cases in programming life. Unit Tests allows you to make big changes to code quickly. The main goal of unit testing is to segregate each part of the program and test that the individual parts are working correctly. Failure in a unit test shows without a doubt which part of the code doesn’t work. The best part is that you don’t need to run whole application to execute unit tests. Whatever language you use, writing unit tests is considered to be one of the best practice.

In this blog, I will explain different ways to write unit tests in Rust.

1) By creating test modules:-

pub struct Rectangle {}

impl Rectangle {
    pub fn area(height: i32, length: i32) -> i32 {
        height * length
    }
}

#[cfg(test)]
mod tests {
    use super::Rectangle;

    #[test]
    fn area_works() {
        let result = Rectangle::area(5, 4);
        assert_eq!(result, 20);
    }
}

You have to create a module named tests and annotate with [cfg(test)]. This annotation instructs Rust compiler to run the test-cases when cargo test command executes. We have added a line use super::Rectangle;, which is used to bring the function(which we want to test) in the scope of tests module.

2) By writing Documentation comments:-

pub struct Rectangle {}

impl Rectangle {
    /// Calculate area of rectangle
    /// # Example
    ///
    /// ```
    /// use rust_examples::Rectangle;
    /// let result = Rectangle::area(5, 4);
    /// assert_eq!(result, 20);
    /// ```
    ///

    pub fn area(height: i32, length: i32) -> i32 {
        height * length
    }
}

Rust gives you flexibility to write documentation comments by using /// . It supports markdown notation for formatting the text. You can add test as documentation comments as shown in above example and run using cargo test command.

Let’s run both tests:


running 1 test
test tests::area_works ... ok

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

     Running target/debug/deps/rust_examples-f7fe3dad91b071e7

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests rust_examples

running 1 test
test src/lib.rs - Rectangle::area (line 7) ... ok

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

As you can see, we got three outputs. We got first output from test module. We can ignore second one, as it is for integration tests. Third output, we got for tests, which we documented.

I hope you enjoy reading this blog and get some idea of writing unit tests in Rust. Happy reading !!


Knoldus-blog-footer-image

Written by 

Ayush is the Sr. Lead Consultant @ Knoldus Software LLP. In his 10 years of experience he has become a developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality.

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!