c++ - Gtest with large C and C ++ codebase -


i project have large codebase , has no unit tests framework @ all.the code working on run on box acts switch/router/firewall.

so working on piece of code needs unit-tested using gtest. problem have mocking variables in order test function itself. eg have function uses 4 pointers different objects , uses couple of global variables .in order test different paths in code need initialize entire state machien/values of dependent variables. adding complexity true in large codebase function/method have written uses bunch of other routines/methods needs tested well. each of needs uni-tested each of them having own dependencies. not sure whether approching problem right or case gtest may not right tool testing such large code-base.

if has experience testing call-stack say

function {     code      code     function b     code      code     function c     code }  function b {     function d     code     function e }  function c{     code     function f     function g     code } 

something this.how test these function a-f ?? strategy ??

first thing refactoring code testable pieces isolated. in particular, means removing access globals. example:

int global; int function() {     int r = foo();     global += r / 2;     bar(r);     return 42; } 

removing global object means converting input parameter:

int real_function(int* target) {     assert(target);     int r = foo();     *target += r / 2;     bar(r);     return 42; } 

then of course remaining code stop compiling, add backward-compatibility cludge:

int global_bar; // @deprecated, use real_function() directly int function() {     return real_function(&global_bar); } 

using that, step call chain, extracting dependencies , 1 day removing last call variant requires globals. in meantime, can write tests functions don't depend on global stuff longer. note c++, use references instead of pointers , pass required external objects class constructor. called dependency injection, make sure research term thorough understanding.

another way test functions touching globals use setup function of test reset global known state. still requires linking in global though, may prove difficult. , not using globals may make codebase better in first place, accepting sends wrong message.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -