Unit testing - how do I test a function that returns random output?

后端 未结 6 835
半阙折子戏
半阙折子戏 2020-12-07 01:01

I have a function which takes in two parameters, and returns one or the other 50% of the time.

The unit test for this should determine that both parameters could be

6条回答
  •  被撕碎了的回忆
    2020-12-07 01:05

    You can do one of two things:

    1. make a stub random() generator that can return consistent numbers when unit testing.
    2. run your code N times, where N is high enough for you to get consistent results so long as the code is working.

    Option #1 is nearly always preferred. By including your random number generator you're no longer testing an atomic unit of code, and by including a stochastic component into unit testing, you need to do statistics to verify that the code is working correctly. There are some bugs that option #2 can never pick up in a reasonable number of tests, which is worrying.

提交回复
热议问题