单元测试 - 如何测试返回随机输出的函数? [英] Unit testing - how do I test a function that returns random output?
问题描述
我有一个函数,它接受两个参数,并在 50% 的时间内返回一个或另一个.
I have a function which takes in two parameters, and returns one or the other 50% of the time.
对此的单元测试应确定可以返回两个参数.幸运的是,我不需要证明每个参数的概率是 50%,但我确实需要证明两个参数都可以返回.
The unit test for this should determine that both parameters could be returned. Luckily, I don't need to prove that the probability for each is 50% but I do need to show that both parameters are possible to be returned.
如何为这个函数编写测试用例?
How do I write a test case for this function?
推荐答案
如果随机性是基于它调用的随机数生成器,您可以装配一个存根 random() 函数,该函数返回各种结果,为您提供预期的结果输出.
If the randomness is based on a random number generator that it calls, you can rig up a stub random() function that returns various results to give you your expected outputs.
这篇关于单元测试 - 如何测试返回随机输出的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!