unit testing Coverage Issue

962 views Asked by At

my ultimate goal is to cover the below code (making complete code green). I am using cantata tool.

#define CHECK1 ((a == 1) || (a == 4))


void check_fun(int a, int b)
{
                if((!CHECK1)&&(b>0)&&(b<10))
                {
                }
}

I have written 5 test cases as below

    void test_check_fun(int doIt){
if (doIt) {
    /* Test case data declarations */
    int a;
    int b;


    START_TEST("test_check_fun_001",
               "<Entry condition: True check for if((!((a == 1) || (a == 4)))&&(b>0)&&(b<10)) ((!(F||F))&&(T)&&(T)) equalent to (T&&T&&T)>");

        /* Expected Call Sequence  */
        EXPECTED_CALLS("");

                /* Set global data */
                initialise_global_data();
                a = 3;
                b = 5;

                /* Set expected values for global data checks */
                initialise_expected_global_data();

            /* Call SUT */
            check_fun(a, b);

            /* Test case checks */
            /* Checks on global data */
            check_global_data();
        END_CALLS();
    END_TEST();

        START_TEST("test_check_fun_002",
               "<Entry condition: False check for if((!((a == 1) || (a == 4)))&&(b>0)&&(b<10)) ((!(T||F))&&(T)&&(T)) equalent to (F&&T&&T)>");

        /* Expected Call Sequence  */
        EXPECTED_CALLS("");

                /* Set global data */
                initialise_global_data();
                a = 1;
                b = 5;

                /* Set expected values for global data checks */
                initialise_expected_global_data();

            /* Call SUT */
            check_fun(a, b);

            /* Test case checks */
            /* Checks on global data */
            check_global_data();
        END_CALLS();
    END_TEST();

        START_TEST("test_check_fun_003",
               "<Entry condition: False check for if((!((a == 1) || (a == 4)))&&(b>0)&&(b<10)) ((!(F||T))&&(T)&&(T)) equalent to (F&&T&&T)>");

        /* Expected Call Sequence  */
        EXPECTED_CALLS("");

                /* Set global data */
                initialise_global_data();
                a = 4;
                b = 5;

                /* Set expected values for global data checks */
                initialise_expected_global_data();

            /* Call SUT */
            check_fun(a, b);

            /* Test case checks */
            /* Checks on global data */
            check_global_data();
        END_CALLS();
    END_TEST();

        START_TEST("test_check_fun_004",
               "<Entry condition: False check for if((!((a == 1) || (a == 4)))&&(b>0)&&(b<10)) ((!(F||F))&&(F)&&(T)) equalent to (T&&F&&T)>");

        /* Expected Call Sequence  */
        EXPECTED_CALLS("");

                /* Set global data */
                initialise_global_data();
                a = 3;
                b = -1;

                /* Set expected values for global data checks */
                initialise_expected_global_data();

            /* Call SUT */
            check_fun(a, b);

            /* Test case checks */
            /* Checks on global data */
            check_global_data();
        END_CALLS();
    END_TEST();

        START_TEST("test_check_fun_005",
               "<Entry condition: False check for if((!((a == 1) || (a == 4)))&&(b>0)&&(b<10)) ((!(F||F))&&(T)&&(F)) equalent to (T&&T&&F)>");

        /* Expected Call Sequence  */
        EXPECTED_CALLS("");

                /* Set global data */
                initialise_global_data();
                a = 3;
                b = 11;

                /* Set expected values for global data checks */
                initialise_expected_global_data();

            /* Call SUT */
            check_fun(a, b);

            /* Test case checks */
            /* Checks on global data */
            check_global_data();
        END_CALLS();
    END_TEST();
}}

and result is showing as

===========================================================================
= Cantata Test Harness v6.2                                               =
= (c) 2012 QA Systems GmbH                                                =
=-------------------------------------------------------------------------=
= Test Description: check                                                 =
=         Log File: test_check.ctr                                        =
=     Test Started: Wed Jun 24 12:10:56 2015                              =
===========================================================================
===========================================================================
= Test Finished: Wed Jun 24 12:10:56 2015                                 =
=-------------------------------------------------------------------------=
= Test                 Script  Checks  Checks  Checks  Call Seq     TEST  =
= Case                 Errors  Failed  Passed  Warned  Failures    RESULT =
=-------------------------------------------------------------------------=
= test_check_fun_001        0       0       0       3         0      PASS =
= test_check_fun_002        0       0       0       3         0      PASS =
= test_check_fun_003        0       0       0       3         0      PASS =
= test_check_fun_004        0       0       0       3         0      PASS =
= test_check_fun_005        0       0       0       3         0      PASS =
= Other                     0       0       4       0         0      PASS =
=-------------------------------------------------------------------------=
= TOTALS                    0       0       4      15         0      PASS =
===========================================================================

my problem is some of the code is not showing in green. so please tell me what test cases I have missed to cover below condition

if((!((a == 1) || (a == 4)))&&(b>0)&&(b<10))

please help me in this. thanks in advance.

1

There are 1 answers

1
forsvarir On

This is an example of why code coverage is a bit of a grey measure. It's useful as an indicator, but it's not transparent.

If I cut and paste the code into C# and run the same tests, I get 100% code coverage, so what's the difference?

Depending on your compiler settings (you should be testing for coverage with all compiler optimisations turned off), the C compiler may actually be creating extra code in order to optimize certain flows, so even though in your logic the (b<10) is only one check, it might result in two separate code instructions used by different flows through the rest of the logic. How this is reported will depend on how your code coverage tool evaluates the coverage.

If you really want to figure out what is causing the coverage level to drop, then you could try removing the different elements of the check in order to see what impact it has. You could also try rearranging your formula. All things being equal, you're better off having simple failures first in your logic because it reduces the number of flows through the code. So in the following version, although the outcome should be the same, the code may be built differently due to the &&s being before the ||.

if((b>0)&&(b<10)&&(!((a == 1) || (a == 4))))

You could also go as far as to remove the parenthesis to see if that improves the coverage. The following returns the same results, whilst making it more explicit what you're testing.

if (b<10 && b>0 && a!=1 && a!=4)