Unit testing a method with random behaviour

前端 未结 6 385
梦如初夏
梦如初夏 2020-12-17 08:44

I am writing unit test cases for a game I am working on. When the game starts, the player is positioned randomly, and I have two problems with that:

  1. Since the
6条回答
  •  我在风中等你
    2020-12-17 09:28

    Make the source of randomness an input to the test, and configure it to be the same each time.

    Almost all random number generators take a 'seed' value. By providing the same seed value each time, you can know that you will get the same sequence of random numbers and hence the output of your application should be exactly the same.

    I have to test all situations in one test case.

    Why? You can have multiple test cases. You can pick any number of different and arbitrary random number seeds to create the conditions you want to test. Or simply replace the random positioning with a specific positioning for the purposes of the test.

提交回复
热议问题