103

We have this stupid library at work. Its called Randomness, and its basically just a wrapper over the standard .NET System.Random class to help our devs generate random data for unit tests easier.

Debate about random data in unit tests aside.

I came across a bug in randomness. Theres a PickFrom function which gets passed an array, and returns a random element from the array. Problem is, it uses Random.Next to do this, and the max value was set to the array length, minus one.

Random.Next generates values inclusive of the min value, and exclusive of the max value. Arr.Length-1 as a max value, is wrong, the last element in the array would never be selected.

So i fixed it.

And proceeded to break dozens upon dozens of unit tests that were now testing from their full sets of data, and had actually been faulty for god knows how long.

Comments
  • 9
    Well done!
  • 30
    I love it!

    Next question is, are people mad at you for 'breaking' their tests?
  • 3
    I though unit tests were supposed to test data whose output you know, I'm having a hard time seeing why including a Randomness thing? Can someone explain it in under 1000 characters?
  • 3
    @Gnonpi my guess is that they have fixed values in that array but to improve test time they only test one random one? Maybe? I don't see any other reason to not just test them all.
  • 1
    @PaperTrail testers are supposed to break things. More things you break, sooner you'll have a promotion
  • 0
    Anyways a test function shouldn't use random values. Tests have to be predictable, you mustn't use that function at all
  • 1
    You could say that...
    (•_•) / ( •_•)>⌐■-■ / (⌐■_■)
    You unveiled the illusion

    YYEEEEAH!
  • 1
    @crisz Exactly. Had a recent situation where my code changes rightfully broke several integration tests for another team. The area manager came unhinged instead of "Great! The integration tests did their job!"

    After a cooling off period and I modified the tests for his team, all was good.
    I don't know why, but some individuals think unit tests is a golden idol that is never supposed to change or heaven forbid, break.
  • 1
    @PaperTrail Hah! The other day I was writing some unit tests that were overly verbose, but there was some a small piece of functionality I wanted to verify before moving forward, so I wrote a few unit tests for it anyway.

    A couple days later they broke, like I knew they would, because I changed things.

    SO I JUST FUCKING DELETED THEM FJDJSKCKDKSKS OH GOD THE HORROR I'M A MONSTER
Your Job Suck?
Get a Better Job
Add Comment