Unit testing algorithms that involve random numbers

拈花ヽ惹草 提交于 2019-12-05 11:56:14

You can use a mocking framework to mock your random number generation. This way you remove the randomness from your result and will be able to test your code with a static set of pre defined test cases.

In all cases you aren't testing the random number generation, but the calculations you are making. And if you whould have a fault you realy need to know the random numbers used to reproduce the fault.

Just select a seed and a number for some iteration (aka how many times you call that PRNG before actually using the values from it), and use these same data (seed & iteration) in your main code and in the unit tests. These data could be in a config file that can be both accessed by the main code and the unit tests.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!