Unit testing in rust

What is test?

Testing is a process by which we assure that the code we have written for our software is working in expected manner. Basically there are two types of test. One is unit test that we going to cover in this blog and another is integration test.

What is unit testing?

It is a testing process where we test the smallest possible part of a software. It usually has one or a few inputs and usually a single output. In procedural programming a unit refers to a individual program, function, procedure etc. For a object oriented programming a unit refers to method belong to super class, abstract class or child class.

How to do it?

Rust looks for all unit tests in the src/ directory. I would suggest maintain this file hierarchy for unit tests. Inside your src directory create a tests directory and then write all tests inside it and don’t forget to import all test in the mod.rs file.

├── Cargo.lock
├── Cargo.toml
├── src
│   ├── main.rs
│   └── tests
│       ├── mod.rs
│       └── unit_test.rs

There are some macros we use while testing:

  • assert!(expression) – panic if false.
  • assert_eq!(left, right) and assert_ne!(left, right) – testing left and right expressions for equality and inequality respectively.

Here below an example of unit test in the rust. Inside your src/main.rs

mod tests;

fn main() {
    println!("Hello, world!");
pub fn add(a: i32, b: i32) -> i32 {
    a + b
pub fn bad_add(a: i32, b: i32) -> i32 {
    a - b

Unit test go into the test module with the #[cfg(test)]. Inside your src/tests/unit_test.rs

mod tests {
    use crate::*;

    fn test_add() {
        assert_eq!(add(1, 2), 3);

    fn test_bad_add() {
        assert_eq!(bad_add(1, 2), 3);

Tests can be run with cargo test.

$ cargo test
   Compiling unit_test v0.1.0 (/home/aniruddha/Desktop/Rust lang/unit_test)
    Finished dev [unoptimized + debuginfo] target(s) in 0.30s
     Running target/debug/deps/unit_test-4578ae065cfb1e10

running 2 tests
test tests::unit_test::tests::test_add ... ok
test tests::unit_test::tests::test_bad_add ... FAILED


---- tests::unit_test::tests::test_bad_add stdout ----
thread 'tests::unit_test::tests::test_bad_add' panicked at 'assertion failed: `(left == right)`
  left: `-1`,
 right: `3`', src/tests/unit_test.rs:12:9
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.


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

You can ignore test also. Here below the example –

mod tests {
    use crate::*;

    fn ignored_test() {
        assert_eq!(add(0, 0), 0);

Here is the result –

$ cargo test
   Compiling unit_test v0.1.0 (/home/aniruddha/Desktop/Rust lang/unit_test)
    Finished dev [unoptimized + debuginfo] target(s) in 0.35s
     Running target/debug/deps/unit_test-4578ae065cfb1e10

running 1 test
test tests::unit_test::tests::ignored_test ... ignored

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

Thank you 🙂

Customize your vim like any other IDE

So, almost 2 years ago I heard about C programming language from my close friend and I thought I should give it a try and I asked “how can I start with it?” and he gave me some notes and said just install turboC++ and start writing code. So, I installed it and started with C language and eventually I got demotivated and quit programming after some time. Actually it was not fun and engaging with that environment. After one year in my college their was a seminar of network security and the guest speaker said that everyone should try python once and it is really amazing language. And I went home and searched for the python in the YouTube and found lots of tutorial on it and picked up one and that tutorial is using VSCode after that I loved that text editor and still I use it in my daily work.

In my recent days I got to know about a new text editor called vim and I recently installed and start using it. The most funny thing is when I first open a file with it I was not able to exit from it :p. Now I use it in most of the time while writing code. When you will first install it then it will look something below like this

The vim allow us to customize it however we want and we customize it by writing some code in the ~/.vimrc and if the file is not there you can add it by doing touch ~/.vimrc command. Now add below line to your file first.

syntax enable
set tabstop=4
set shiftwidth=4
set expandtab
set number
filetype indent on
set autoindent
set ruler
set showcmd
set incsearch
set hlsearch

Now we will add some plugins in this file and then we are going to install it.There are couple of ways to install plugins and the way i like is by Vundle And the section below shows how to add plugins .

set nocompatible   
filetype off                  

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

Plugin 'VundleVim/Vundle.vim'
Plugin 'wting/rust.vim'
Plugin 'racer-rust/vim-racer'
Plugin 'scrooloose/nerdTree' "Documents tree
Plugin 'davidhalter/jedi-vim' "python autocompletion
Plugin 'tpope/vim-fugitive' "git integration
Plugin 'scrooloose/syntastic' "code syntaxis
Plugin 'Raimondi/delimitMate' "automatic closing of quotes, parenthesis...
Plugin 'yggdroot/indentline' "Show indent lines (useful for loops)
Plugin 'tmhedberg/SimpylFold' "Python folding
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
Plugin 'ervandew/supertab' "<Tab> for code completion
Plugin 'ctrlpvim/ctrlp.vim'    "Finder for vim
Plugin 'jistr/vim-nerdtree-tabs'

call vundle#end()   

filetype plugin indent on

That’s how we add Plugins to the vim by writing Plugin and after that what plugin we want to install. Now to install plugins first type Esc then : and after that type PluginInstall then press enter and all the plugins will install one by one.

Now vim is looks something like blow image.

Thank you 🙂