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.