I have a class PlayingCard which represents a particular playing card. I have another class, Deck, which contains a list of PlayingCard objects. Deck has a shuffle()
method that randomizes the card order.
I would like to write some unit tests for the shuffle() method, but I'm at a bit of a loss. I'd prefer the test to not care about the internals of just how the shuffle is done, but I want them to be good tests.
How do I best unit test when randomness is involved?
One approach is to make statistical tests; after each shuffle check for correctness (the set of cards must not have been changed, only the order), and collect statistics about some random variables ("position of the 7 of diamonds", "is the 5 of clubs before or after the 8 of hearts", and the like) to be tested after a suitable number of shuffles by Student's t-test and other statistical hypothesis-testing approaches.
I don't have particular ideas about unit testing this, but a quick note about the algorithm you use. It is very easy to naively and unknowingly create a biased shuffle algorithm. No need to reinvent the wheel—the Fisher-Yates shuffle will guarantee an unbiased shuffle, if implemented correctly.
There are easy pitfalls you might hit if you don't do FY properly:
mod 52
to get random card positions. Also leads to slight bias.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With