Unit testing a method with random behaviour

前端 未结 6 376
梦如初夏
梦如初夏 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:08

    One approach you can take is to split out the random-position generation into a separate class/interface, so that you can override it in your test, and therefore control it.

    0 讨论(0)
  • 2020-12-17 09:11

    There are two different things to test here, and you should test them separately:

    • Does the program logic work for all possible starting positions? Test this by positioning the player non-randomy at a number of positions, trying to cover all "special cases".
    • Is the random positioning actually random? Test this by calling only the positioning logic many times, and do some sort of statistical analysis.
    0 讨论(0)
  • 2020-12-17 09:16

    According to testing theory it is not possible to test random behavior :) On the other hand, as in previous answers said, for your tests you could make pseudo random activities somehow.

    0 讨论(0)
  • 2020-12-17 09:18

    I suggest you treat your source of randomness (a random number generator or whatever) as a dependency. Then you can test it with known inputs by providing either a fake RNG or one with a known seed. That removes the randomness from the test, while keeping it in the real code.

    If you fake the RNG, you can test what happens if it would naturally position the player on an obstacle - how it moves the player out of the way, etc. Of course that relies on knowing how the class uses the RNG, but personally I'm happy enough with unit tests acting as "white box tests" with some internal knowledge.

    0 讨论(0)
  • 2020-12-17 09:18

    As other answers said, random algorithms are deterministic. you can reproduce a sequence if you use the same seed.

    The only situations where I had to deal with a non-deterministic code, is when multithreading was involved. Then, you are dependant on the scheduler of the operating system.

    In those cases, I write the unit-test as a loop, that repeats thousand of times the test code.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题