Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

randomness in test data

 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i am having a unit test, which uses a list of objects as test data.

i am testing that this list forces a method to be called a certain amount of times depending on the list's size and objects type inside the list. i am using a mocks to isolate my class.

because i want to have random objects in this list i am calling a build method which generates a certain number of fixed objects whose type is depending on a int return value of a random function.

now my question:
is it alright if some randomness is included in my test data or should it be avoided at any time?

pros:
-i use random (in my case realistic) test data.
cons:
-a failing test is very difficult (or even impossible) to reproduce.
-unit tests shouldn't be dynamic, running them once or thousand times must show the same results, which is not 100% sure if test data can change during different test runs

of course i would use only random data in very rare cicumstances! but i thought it my case useful, because it is very time consuming and annoying to generate a good mixed and big list in my test suite.

maybe the solution to my problem could be a little code generater and copy/paste it into my tests...

thanks for your thoughts.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe the solution to my problem could be a little code generater and copy/paste it into my tests...

That's probably what I would do in this case.

Before that, though, I would investigate to see if I could repeatably "seed" the random number generator to produce the same pseudo-random sequence each time. If necessary, you could then fairly easily repeat the test for different seeds.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34680
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manuel,
For unit tests, I prefer to use the same data each time as it results in repeatable tests. For integration tests, I don't mind some randomness.
 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, i got your points.
 
Jack Lee
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manuel aldana,

My suggestion is when you have no choice but use randomness in your unit testing, you can write extra code to log the list information when a failure occurs, which may make it easy to reproduce the failure.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic