mercredi 29 mai 2019

Should we modify a function signature for unit testing?

Suppose I have a function add() as below:

void add(int a, int b) {
    int sum=a+b;
    cout<<sum;
    sendSumToStorage(sum);
}

This simple function adds to input values, prints the sum to the console and also sends it to some external storage (say, a file). This is how we ideally want it in the application (meaning, we don't want it to return anything).

For purposes of unit testing, is it valid (from a design perspective) if I modify the function signature so that it returns the sum? I could then have a test like:

bool checkAdd() {
    in res=add(3, 4);
    if(res==7) return true;
    else return false;
}

Better yet, is this (returning a value) the only way we could unit test it? Is there some valid way in which we could unit test the add() function without changing the function signature?

Aucun commentaire:

Enregistrer un commentaire