Unit testing a method with random behaviour
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:
- Since the player is positioned randomly, I cannot be sure that a test case that passes once will pass again. For example, it could pass most of the time, but fail if the player happens to be positioned in front开发者_运维技巧 of an obstacle.
- I have to test all situations in one test case. For example, when testing whether the player moves correctly, I have to check whether there was an obstacle and if it was considered by the algorithm.
I'm not really happy with that, but I don't see a way out. Is it acceptable to test methods with partially random behaviour?
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.
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.
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.
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.
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.
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.
精彩评论