views:

47

answers:

3

Is there a sane way to unit test a stochastic process? For example say that you have coded a simulator for a specific system model. The simulator works randomly based on the seeds of the rngs so the state of the system cannot be predicted and if it can be every test should bring the system to a specific state before it attempts to test any method of a class. Is there a better way to do this?

+1  A: 

Here's a nice blog post that covers this topic. Basically you will need to inject a controlled randomness into the object under test.

Darin Dimitrov
A: 

Maybe you could use JUnit Theories to solve that.

http://blogs.sun.com/jacobc/entry/junit_theories

cherouvim
A: 

The two obvious choices are to remove the randomness (that is, use a fixed, known seed for your unit tests and proceed from there), or to test statistically (that is, run the same test case a million times and verify that the mean and variance (etc.) match expectations). The latter is probably a better test of your system, but you'll have to live with some false alarms.

Drew Hall