“100% code-coverage” doesn’t mean everything is tested completely and how to add it in FuelPHP

Lemon Kazi
Oceanize Lab Geeks
Published in
3 min readMay 6, 2019

Code Coverage:

Code coverage is a measurement of how many lines or blocks of your code are executed while the automated tests are running and how well your test set is covering your source code.

There are various coverage area, like paths, conditions, statements, etc..

The code coverage is just a percentage of tests that execute each of these coverage criteria.

How it easier to manage 100% coverage than 90-something percent coverage?

Code coverage covered under tests. So, if you get 90% code coverage than it means that there is 10% of code that is not covered under tests. If you think that 90% of the code is covered but you have to look from a different angle. What is stopping you to get 100% code coverage?

A good example will be this:

if(customer.IsNewCustomer()) 
{
}
else
{
}

Now, in the above code there are two paths. If you are always hitting the “YES” then you are not covering the else part and it will show in the Code Coverage results.

This is good because you know what is not covered and you can write a test to cover the else part.

Basically, 100% code-coverage doesn’t mean your code is perfect. Use it as a guide of comprehensive (unit-)tests.

But we also realized that 100% code coverage is sometimes an artificial goal.

It would be very easy to get 100% code coverage for the following function.

function div($number, $denominator) {
return $number/ $denominator;
}

How to add Code Coverage in PHPUnit (FuelPHP):

I used FuelPHP so here I will explain how to add code coverage with PHPUnit.

  1. At first you have to add PHPUnit in composer.json.

2. You have to add below code in phpunit.xml in this directory fuel/app

<logging>
<log type=”coverage-html” target=”../../public/coverage”
lowUpperBound=”35" highLowerBound=”70"/>
</logging>

So your final phpunit.xml will be

3. Now you have to write test code. Here I just added a method testing sample from model.

test file location will be. fuel/app/tests/model/admins.php

4. So all have we done. Now you have to run a command like this

php oil test

It will generate code coverage html like below in your destination you set on phpunit.xml

--

--