Bid adieu to tarpaulin, HTML reports are here for RUST

If you bumped into this article, you might not be new to the Rust language. So, if you are not new then you might have written test cases for your code (for sure!!) and to test your code coverage, you were likely to use tarpaulin tool.

But hey… think about this – You are ready with your code base with all the best practices incorporated (That’s important dude!!). Now you want to push your code to a central repository like GitHub. How will you add your test cases coverage report to the repository? In the form of a screenshot? Naaah. In a text file? Bruh!

Image result for rust language

Kcov: A code coverage tool

Kcov is a code coverage tool for Binaries, Shell scripts as well as Python scripts. It generates an HTML file for most of the languages. But we will focus mainly on Rust language. You might argue that tarpaulin gives you a very decent code coverage format. But we should always thrive for the best.

A sample Code

Consider this code, which has 2 functions to calculate the cube and square of a number.

#[cfg_attr(tarpaulin, skip)]
fn main() {

    let number: u32 = 5;
    println!("Square of {} is {}", number, square_of_integer(number));
    println!("Cube of {} is {}", number, cube_of_integer(number));
}

fn square_of_integer(operand1: u32) -> u32
{
    let result = operand1 * operand1;
    return result;
}

fn cube_of_integer(operand1: u32) -> u32
{
    let result = operand1 * operand1 * operand1;
    return result;
}

Test Cases

Test cases for the above code

#[test]
   fn test_square_of_integer() {
       assert_eq!(square_of_integer(2), 4);
  }

#[test]
   fn negative_test_square_of_integer() {
       assert_ne!(square_of_integer(3), 100);
  }

#[test]
   fn test_cube_of_integer() {
       assert_eq!(cube_of_integer(11), 1331);
  }

#[test]
   fn negative_test_cube_of_integer() {
       assert_ne!(cube_of_integer(5), 1225);
  }

Using Tarpaulin

Now we run the tarpaulin command to check the test coverage result.

cargo tarpaulin -v

Finally, this is the code coverage result after running tarpaulin command.

running 4 tests
test negative_test_cube_of_integer ... ok
test negative_test_square_of_integer ... ok
test test_cube_of_integer ... ok
test test_square_of_integer ... ok

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

[INFO tarpaulin] Coverage Results:
|| Uncovered Lines:
|| Tested/Total Lines:
|| src/main.rs: 14/14
|| 
100.00% coverage, 14/14 lines covered

Firstly, it gives you the number of test cases passed. Secondly, it displays the number of lines covered. Lastly, it gives you the percentage of code coverage. Although, Its very easy to see the results for one application.

Why use Kcov?

But what will you do when you have more than one application for which you have to see the test coverage? Can you exclude a specific file while using tarpaulin? Or can you merge two application test cases?

The answer would be no until you use an advanced code coverage tool like kcov which helps you answer all the above questions by giving you a well prepared HTML report for coverage results.

Installing Kcov

Kcov requires an installation if not already present on your machine. This blog will be telling you about the installation on Linux OS.

Firstly, Enter the following commands in the terminal for installing dependencies for Kcov.

sudo apt-get install libcurl4-openssl-dev libelf-dev libdw-dev cmake gcc binutils-dev libiberty-dev zlib1g-dev

After that, run the following commands on your terminal.

wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz
tar xzf master.tar.gz
cd kcov-master
mkdir build
cd build
cmake ..
make
sudo make install

This will install the Kcov tool to your system.

The Final Step: Generating HTML Report

To collect coverage data first, generate your test executable by running the following command.

cargo test --no-run

The compiled binaries for test cases are saved in target/debug.

Finally, run the command for HTML Report generation.

kcov --exclude-pattern=[comma separated list of pattern to be excluded]/--include-pattern=[comma separated list of pattern to be included] --verify cov target/debug/<executable name>

The compiled coverage report can be seen in target/cov as index.html.

HTML Report

The HTML file shows you how many times the line was hit by the test cases and in which order.

This image gives the coverage results in percentage
This image shows the number of hits to every line and the order in which it was hit

Concluding it, Tarpaulin is easy to use by a beginner but is less flexible. So, the need of the hour is HTML report generation because of its great amount of features and ability to show you the results in GUI format which can be saved on GitHub or any other platforms.

The example project is here for reference https://github.com/knoldus/rust-kcov

Thanks for reading. Happy Blogging…

knoldus-advt-sticker

1 thought on “Bid adieu to tarpaulin, HTML reports are here for RUST

Leave a Reply

Knoldus Pune Careers - Hiring Freshers

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