随机过程的单元测试?
有没有一种合理的方法来对随机过程进行单元测试?例如,假设您已经为特定系统模型编写了模拟器。模拟器根据 rng 的种子随机工作,因此无法预测系统的状态,如果可以的话,每次测试都应该在尝试测试类的任何方法之前使系统进入特定状态。有更好的方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
两个明显的选择是消除随机性(即,使用固定的已知种子进行单元测试并从那里继续),或进行统计测试(即运行相同的测试用例一百万次并验证平均值和方差(等)符合预期)。后者可能是对您的系统的更好测试,但您将不得不忍受一些误报。
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.
这是一篇不错的博文涵盖了这个主题。基本上,您需要将受控随机性注入到被测试的对象中。
Here's a nice blog post that covers this topic. Basically you will need to inject a controlled randomness into the object under test.
也许您可以使用 JUnit
Theories
来解决这个问题。http://blogs.oracle.com/jacobc/entry/junit_theories
Maybe you could use JUnit
Theories
to solve that.http://blogs.oracle.com/jacobc/entry/junit_theories
您需要找到系统的 Q0 和 p00。 p00 是预测状态,而 qo 是
计算出的状态。预测的状态可以找到循环系统,即
系统中的最小值,即 k。
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.
如果您的模型是随机的,那么您可以将输出视为随机生成的样本。然后,在单元测试功能中,您可以使用置信区间执行某种假设检验。如果测试输出在置信区间内,则测试成功。然而,有可能产生假阳性/假阴性。
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.