What is code coverage and how do YOU measure it?

后端 未结 9 1852
长情又很酷
长情又很酷 2020-12-04 04:39

What is code coverage and how do YOU measure it?

I was asked this question regarding our automating testing code coverage. It seems to be that, outside of automated

9条回答
  •  隐瞒了意图╮
    2020-12-04 05:06

    Code coverage basically tells you how much of your code is covered under tests. For example, if you have 90% code coverage, it means 10% of the code is not covered under tests.

    I know you might be thinking that if 90% of the code is covered, it's good enough, but you have to look from a different angle. What is stopping you from getting 100% code coverage?

    A good example will be this:

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

    Now, in the code above, there are two paths/branches. If you are always hitting the "YES" branch, you are not covering the "else" part and it will be shown in the Code Coverage results. This is good because now you know that what is not covered and you can write a test to cover the "else" part. If there was no code coverage, you are just sitting on a time bomb, waiting to explode.

    NCover is a good tool to measure code coverage.

提交回复
热议问题