In Rust, there are specific procedures that are needed to be adhered to before writing the tests. The steps for writing the tests are as follows:
Step 1: Setting up data/states required by our code.
Step 2: Run the code for the tests we need to test.
Step 3: Asserting the expected results.
In Rust, we annotate the functions with the test attributes which are nothing but metadata about the information about Rust.
To run tests, we use the cargo command. When running the tests, the cargo compiler builds the test runner binary, and then it reports to us whether the annotated function passes or fails.
Now we take an example, we create a library project called multiply that multiplies two numbers.
cargo new multiply --lib
cd multiply
Example 1:
Rust
#[cfg(test)]
mod tests {
#[test]
fn multiply_function() {
let result = 5*5;
assert_eq!(result, 25);
}
}
Output:
Now, we run the code using cargo test. After running the tests, we get output:
Explanation:
In this example, we use the #test annotation which is an attribute that indicates the test function, hence the test runner in Rust treats this function as a test. We use the assert_eq! macro so that the result of multiplying 5 and 5, equals 25. This assertion helps in running tests. Once, we use the command cargo test, we can see in the output that there is a test running called to multiply the result which is Ok which implies that the tests have successfully
Example 2:
Rust
#[cfg(test)]
mod tests {
#[test]
fn test_one() {
let result = 5 * 5;
assert_eq!(result, 25);
}
#[test]
fn test_two() {
panic!("test_two is failed");
}
}
Output:
On running the cargo test, we get the following output
Explanation:
In this example, we have continued from example 01 where we have used the multiply function. Here, we have used two tests named test_one and test_two. We have used the panic! keyword intentionally here so that one of the tests fails while test_one gets passed (i.e. asserted correctly)
Similar Reads
Rust - Traits A trait tells the Rust compiler about functionality a particular type has and can share with other types. Traits are an abstract definition of shared behavior amongst different types. So, we can say that traits are to Rust what interfaces are to Java or abstract classes are to C++. A trait method is
8 min read
Rust - Strings String data type is a very important part of any programming language. Rust handles strings a bit differently from other languages. Â The String data type in Rust is of two types: String Literal (&str)String Object (String)String LiteralString Literal or &str are called 'string slices', whic
2 min read
Rust - Generic Traits Rust is an extremely fast programming language that supports speed, concurrency, as well as multithreading. In Rust, we have a concept of Generic Traits where Generics are basically an abstraction of various properties, and Traits are basically used for combining generic types for constraining types
2 min read
Rust - if let Statement If-let statement in Rust is an expression that allows pattern matching and is similar to an if expression. Â Once the pattern condition is matched then the block of code is executed. A scrutinee is an expression that is matched during match statement executions. For example match g{ X =>5 , Y =
2 min read
Variables in Rust Variables are memory addresses used to store information to be referenced and manipulated in programs. They also provide a way of defining data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that
4 min read
Rust Vs OpenCV Rust and OpenCV are both powerful tools in the world of software development, but they serve different purposes and excel in distinct areas. Rust Vs OpenCVThis article will provide a comprehensive comparison of Rust and OpenCV, highlighting their strengths, use cases, and key features.What is Rust?R
4 min read