0

In the C++ project I'm doing at work I have to accomplish high code coverage with gTest/gMock, but the problem is that I also have to use external C library (net-snmp in particular). Since I use that library a lot and and as far as I know C functions can't be mocked I don't see how I can get even close to above 90% line coverage.

For example, majority of my code is like this:

int status = snmp_synch_response(ss, pdu, &response);
if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR)
{
    netsnmp_variable_list *vars = response->variables;
    if (var->type == ASN_OCTET_STR)
    {
        //...
    }
    else if (var->type == ASN_INTEGER)
    {
        //...
    }
    else
    {
        //...
    }
    
    //...
}

Because the call to library function snmp_synch_response is never successful in test this if branch is never entered and the majority of my code is like this.

I'm still a beginner, especially with writing tests, so I'm not sure if I'm missing something obviously here?

Quarra
  • 2,527
  • 1
  • 19
  • 27
Jet Black
  • 21
  • 3
  • One option to is to write an abstract class that hides the calls to snmp. Then you can use gmock to mock a test class, and then write the actual class that calls snmp. – Nick M. Oct 10 '20 at 18:48
  • @NickM. There are a "few" other options without using a wrapper interface. Read through the dupe list please. – πάντα ῥεῖ Oct 10 '20 at 18:51

0 Answers0