1. What is Software Testing?
Software testing is the process of checking whether a program behaves as expected.
For example, suppose you create a function that adds two numbers.
You expect:
Testing allows you to verify this automatically.
Why is Testing Important?
- Finds bugs before users experience them.
- Helps maintain code quality.
- Makes it safer to modify existing code.
- Reduces manual testing work.
- Helps developers understand expected behavior.
2. Types of Testing in Rust
Rust supports different kinds of testing.
Type | Purpose |
|---|---|
Unit Testing | Test individual functions or small code units |
Integration Testing | Test how different parts of an application work together |
Documentation Testing | Test code examples written in documentation |
We will study all three.
3. Your First Unit Test
Rust provides a built-in testing framework.
Let's create a simple function and test it.
Explanation
#[cfg(test)]
This tells Rust to compile the test module when running tests.
mod tests
Creates a module named tests.
use super::*
Imports items from the parent module so the tests can access them.
#[test]
Marks a function as a test function.
assert_eq!()
Checks whether two values are equal.
Run the Test
Open your terminal and execute:
Expected Output
Your first Rust test is successful!
4. Understanding Assertion Macros
Rust provides several assertion macros for testing.
1. assert!
Checks whether a condition is true.
If the condition is false, the test fails.
2. assert_eq!
Checks whether two values are equal.
3. assert_ne!
Checks whether two values are not equal.
Comparison
Macro | Purpose |
|---|---|
| Checks a Boolean condition |
| Checks equality |
| Checks inequality |
5. Test Multiple Functions
You can test multiple functions inside the same test module.
Run:
Expected result: three tests should pass.
6. Test Functions That Return Result
Rust tests can also return a Result.
This is useful when the code being tested can produce errors.
Here, ? returns an error from the test if the operation fails.
7. Test Expected Panics
Sometimes a function is expected to panic under certain conditions.
Rust provides the #[should_panic] attribute.
This test passes because the function is expected to panic.
You can also check the panic message:
8. Test Private Functions
Rust allows tests inside a module to access private functions through the parent module.
Even though calculate_tax() is private, the child test module can access it.
9. Integration Testing
Unit tests usually test small parts of your code.
Integration tests check whether different parts of your application work together through their public interfaces.
Cargo uses a special folder named tests for integration tests.
Project Structure
Step 1: Create a Library Function
Inside src/lib.rs:
Step 2: Create an Integration Test
Create this file:
tests/integration_test.rs
Replace my_project with the actual library crate name from your Cargo.toml, using underscores if the package name contains hyphens.
Step 3: Run Tests
Cargo automatically discovers integration tests inside the tests directory.
10. Documentation Tests
Rust can also test code examples written in documentation comments.
These comments begin with ///.
Cargo runs these documentation examples when you execute:
The code examples in documentation are called documentation tests, or doctests.
11. Run Specific Tests
Cargo provides commands to control which tests run.
Command | Purpose |
|---|---|
| Run all tests |
| Run tests matching |
| Run library unit tests |
| Run a specific integration test target |
| Show printed output from tests |
| Run tests using one test thread |
Example
This runs tests whose names match test_add.
12. Test-Driven Development (TDD)
TDD stands for Test-Driven Development.
It is a development approach where you write tests before writing the actual implementation.
The basic process is:
- Write a test.
- Run the test and observe that it fails.
- Write the code.
- Run the test again.
- Improve the code while keeping the test passing.
Example
First, write a test:
Then implement the function:
Now run:
The test should pass.
13. Testing Best Practices
- Give tests meaningful names.
- Test normal inputs and edge cases.
- Test error conditions.
- Keep tests independent of one another.
- Avoid depending on a specific test execution order.
- Test public application behavior through integration tests.
- Run
cargo testbefore committing important changes. - Avoid relying on external services in ordinary unit tests.
14. Mini Project: Calculator with Tests
Let's create a calculator and test its operations.
Complete Code
Expected Test Result
15. Practice Exercises
Try these exercises:
- Create a function that checks whether a number is even.
- Write tests for even and odd numbers.
- Create a function that finds the maximum of two numbers.
- Write tests for positive, negative, and equal numbers.
- Create a function that calculates student grades.
- Test invalid marks and valid marks.
- Create a function that checks whether a student is eligible for admission.
- Write an integration test for a public function in your library.