Reputation: 693
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:
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?
Upvotes: 28
Views: 8349
Reputation: 11270
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.
Upvotes: 1
Reputation: 872
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.
Upvotes: 0
Reputation: 346309
There are two different things to test here, and you should test them separately:
Upvotes: 2
Reputation: 18449
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.
Upvotes: 3
Reputation: 9101
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.
Upvotes: 6
Reputation: 1500605
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.
Upvotes: 39