13

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?

imoschak
  • 215
  • 2
  • 8

5 Answers5

7

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
  • 28,429
  • 12
  • 61
  • 81
  • 4
    a weakness of the fixed seed approach might be that if you change the algorithm you are testing such that it performs more calls (or less calls) to the random generator, then you will also have to fix the unit test again to meet this change. – Michelrandahl Jun 08 '15 at 17:31
4

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

Emre
  • 5,976
  • 7
  • 29
  • 42
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

Maybe you could use JUnit Theories to solve that.

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

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
cherouvim
  • 31,725
  • 15
  • 104
  • 153
0

you need to find the Q0 and p00 for the system. p00 is the predicted state while qo is the calculated state.the predicted state can lead to find the recurrant system which is the smallest value, say k in the system.

0

If your model is stochastic, then you could treat output as a randomly generated sample. Then, in your unit testing function, you could perform some sort of hypothesis testing with confidence interval. If the test output is within the confidence bound, then the test is successful. However, there will be a possibility of generating false positive/false negative.