java - How to write jUnit tests for random output? -


this question has answer here:

testing code has predefined input , output parameters relatively easy when compared writing tests code has portion of randomness, must check if random generator biased or not.

an example of library uses random numbers java.util.collections.shuffle(list<?> list) shuffles collection of objects, following http://en.wikipedia.org/wiki/fisher%e2%80%93yates_shuffle.

how write junit tests code has random output? not shuffle, testing randomness in general.

unless writing actual random number generator or kind of encryption library relies on secure random number generators don't need check if random number generator biased or not. job of author of random number generator.

your example of collections.shuffle() poor example because built in jdk method. there's no reason test built-in jdk method, authors of java have done plus millions of users have used these methods on past 20 years. have tests confirm system.out.print() works expected too?

unit tests supposed deterministic. rerunning same test on , on again should produce same results every time. if don't , test fails, failing because code buggy or because non-deterministic output produced invalid input? if when rerunning results, tests pass, because bug fixed or because happened random input worked?

for these reasons, unit tests should try mock or stub out randomness. perhaps write several tests perform specific transformations of data test boundary conditions. example, shuffle() maybe have test reorders elements in specific order or sorts elements in ascending or descending order or test doesn't transformation etc. these valid results happen randomly.

this way tests produce same output same input everytime , benefit of different results.

edit appears want test own random number generator.

here links describe statistical tests should see if random (or @ least random possible)

random.org statistical analysis

nist statistical test suite


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -