jeudi 27 juin 2019

Using random numbers vs hardcoded values in unit tests

When I write tests, I like to use random numbers to calculate things.

e.g.

 func init() {
   rand.Seed(time.Now().UnixNano())
 }

  func TestXYZ(t *testing.T) {
     amount := rand.Intn(100)
     cnt := 1 + rand.Intn(10)
     for i := 0; i < cnt; i++ {
       doSmth(amount)
     }
     //more stuff
  }

which of course has the disadvantage that

expected := calcExpected(amount, cnt)

in that the expected value for the test needs to be calculated from the random values.

If have received criticism for this approach:

  • It makes the test unnecessarily complex
  • Less reproduceable due to randomness

I think though that without randomness, I could actually:

  • Make up my results, e.g. the test only works for a specific value. Randomness proves my test is "robust"
  • Catch more edge cases (debatable as edge cases are usually specific, e.g. 0,1,-1)

Is it really that bad to use random numbers?

(I realize this is a bit of an opinion question, but I am very much interested in people's point of views, don't mind downvotes).

Aucun commentaire:

Enregistrer un commentaire