简体   繁体   中英

How to made the code coverage to 100%

Recently I started to write PHPUnit test. And this is my model code.(I used CodeIgniter 3).

Account_model.php

class Account_model extends CI_Model
{
    ...

    public function select_by_seq($seq = '', $select_columns = [])
    {
        try {
            $bind = [':a_seq' => $seq];

            // check $select_colums is exist in table
            if ($this->check_column($select_columns) === false)
            {
                throw new Exception('columns illegal', 201);
            }        

            ...

            $sql = "select ....
                    from {$this->db->dbprefix('account')}
                    where a_seq = :a_seq";

            $query = $this->db->query($sql, $bind);

            // ===== this always not runing. =====
            if ($query === false)
            {
                // ===== this always not runing. =====
                throw new Exception('sql errors', 301); 
            }
            else
            {
                return $query->result_array();
            }
        }
        catch (Exception $error)
        {
            // set error log
            $this->set_error_log($error->getCode() . $error->getMessage());
        }

        return false;
    }
}

This is my test Account_model_test.php

class Account_model_test extends TestCase
{
    public static function setUpBeforeClass()
    {
        parent::setUpBeforeClass();

        $CI =& get_instance();

    }

    public function setUp()
    {
        $this->resetInstance();

        loader('model', 'account_model');

        $this->obj = $this->CI->account_model;
    }

    public function test_select_by_seq()
    {
        $result = $this->obj->select_by_seq(
            $seq = '20160830'
        );

        $this->assertCount(1, $result);
    }

    public function test_select_by_seq_with_illegal_column()
    {
        $result = $this->obj->select_by_seq(
            $seq = '20160830',
            $select_columns = ['illegal']
        );

        $this->assertFalse($result);
    }

    ...
}

Because I write SQL by myself. I founded my PHPUnit test can't cover this if ($query === false) . And then my code coverage didn't achieve 100%. This problem let me think the 100% is very important for the unit test? Or I had to modify my model code? Thanks your help.

It's good to always have code coverage 100%. But like you said there are situations when this is very hard to achieve. So having code coverage greater than 70% is quite good for most of the projects. See this link regarding the minimum code coverage required in project. But you should try to extract your business logic to its own class (Repository) and unit test it.

It would be good if you show us your test file also, so we can be more clear about what you already have there. Without knowing that, what I can suggest is to use Mock of query function, so it can return false as result. You can find more about Mocks here

Also as written before: you should not focus on 100% code coverage.

The @codeCoverageIgnore , @codeCoverageIgnoreStart and @codeCoverageIgnoreEnd annotations can be used to exclude lines of code from the coverage analysis. -- PHPUnit Manual

There are examples in the PHPUnit documentation .

In a nutshell, wrap the code to be ignored by code coverage in *Start *End annotations:

if (false) {
    // @codeCoverageIgnoreStart
    print '*';
    // @codeCoverageIgnoreEnd
}

Also see What is a reasonable code coverage % for unit tests (and why)? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM