vendredi 28 juin 2019

What To Unit Test

I'm a bit confused how much I should dedicate on Unit Tests.

Say I have a simple function like:

appendRepeats(StringBuilder strB, char c, int repeats)

[This function will append char c repeats number of times to strB. e.g.:

strB = "hello"
c = "h"
repeats = 5
// result
strB = "hellohhhhh"

]

For unit testing this function, I feel there's already so many possibilities:

  • AppendRepeats_ZeroRepeats_DontAppend
  • AppendRepeats_NegativeRepeats_DontAppend
  • AppendRepeats_PositiveRepeats_Append
  • AppendRepeats_NullStrBZeroRepeats_DontAppend
  • AppendRepeats_NullStrBNegativeRepeats_DontAppend
  • AppendRepeats_NullStrBPositiveRepeats_Append
  • AppendRepeats_EmptyStrBZeroRepeats_DontAppend
  • AppendRepeats_EmptyStrBNegativeRepeats_DontAppend
  • AppendRepeats_EmptyStrBPositiveRepeats_Append
  • etc. etc. strB can be null or empty or have value. c can be null or have value repeats can be negative or positive or zero

That seems already 3 * 2 * 3 = 18 test methods. Could be a lot more on other functions if those functions also need to test for special characters, Integer.MIN_VALUE, Integer.MAX_VALUE, etc. etc. What should be my line of stopping? Should I assume for the purpose of my own program: strB can only be empty or have value c has value repeats can only be empty or positive

Sorry for the bother. Just genuinely confused how paranoid I should go with unit testing in general. Should I stay within the bounds of my assumptions or is that bad practice and should I have a method for each potential case in which case, the number of unit test methods would scale exponentially quite quick.

Aucun commentaire:

Enregistrer un commentaire