Unit testing code coverage - do you have 100% coverage?

后端 未结 17 2727
离开以前
离开以前 2020-12-13 09:05

Do your unit tests constitute 100% code coverage? Yes or no, and why or why not.

17条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 09:07

    No for several reasons :

    • It is really expensive to reach the 100% coverage, compared to the 90% or 95% for a benefit that is not obvious.
    • Even with 100% of coverage, your code is not perfect. Take a look at this method (in fact it depends on which type of coverage you are talking about - branch coverage, line coverage...):


    public static String foo(boolean someCondition) {
        String bar = null;
        if (someCondition) {
            bar = "blabla";
        }
        return bar.trim();
    }
    

    and the unit test:

    assertEquals("blabla", foo(true));
    

    The test will succeed, and your code coverage is 100%. However, if you add another test:

    assertEquals("blabla", foo(false));
    

    then you will get a NullPointerException. And as you were at 100% with the first test, you would have not necessarily write the second one!

    Generally, I consider that the critical code must be covered at almost 100%, while the other code can be covered at 85-90%

提交回复
热议问题