- I am working on a test project to test a neural networks library…
- The problem is that this library sometimes uses random numbers..
- I need to derive test cases (input,expected output,actual output)…
Does anybody have an idea how to derive test cases (input,expected output,actual output) to a function that uses random numbers when taking actions and evaluating outputs??
Yes, you either have to run a large enough number of cases so that the randomness averages out, or you make the random source another input to your function or method so you can test it independently.
An example of the first kind (this is Python, but the principle can apply in any language).
So this test can fail if you’re unlucky, but it’s still a reasonable test since it’ll pass nearly all the time, and it’s pretty simple to make this kind of test.
To do things ‘properly’, you need to inject the random source.
Now you can unit test this by either mocking out or faking the DefaultRandomBehavior class, thus completely side-stepping the non-determinism.