How would I perform property-based testing on a card game's Deal function?

182 Views Asked by At

I am studying property-based testing and am curious how I would apply this type of testing to a BlackJack game's Deal function.

Here's a unit test (aka: Example-based test):

[<Test>]
let ``deal two cards`` () =
    let hand = 2
    let dealPlayer cardCount = 
        [for i in [1..cardCount] -> shuffleDeck.Pop] 
        |> Seq.toList

    (dealPlayer hand).Length |> should equal (2)

What would be an example of a property-based test?

  • Would I test that the two cards are within a range of 4 suits? (i.e. Spades, Diamonds, Hearts, Clubs) Wouldn't relying on a static type already ensure that for me?

  • Would I test that the two cards are within the range of numbers or faces? (i.e. 2..10;Jack;Queen;King;Ace) Again, wouldn't relying on a static type already ensure that for me?

Again, what should I consider when performing property-based tests for a blackjack game?

1

There are 1 best solutions below

0
On BEST ANSWER

If you model the domain (in this case: a card game) using algebraic data types, the type system takes care of many things for you.

There are already examples of modelling a card game here and here, but in essence, you can model it like this:

type Suit = Diamonds | Hearts | Clubs | Spades
type Face =
    | Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten
    | Jack | Queen | King | Ace

type Card = { Suit: Suit; Face: Face }

This makes illegal states unrepresentable, which means that it doesn't even make sense to test whether a Suit is one of Diamonds, Hearts, Clubs, or Spades; these are the only values it can have.

what should I consider when performing property-based tests for a blackjack game?

I don't know, that's for you to figure out (I also don't know the rules for Blackjack), but here's an example of property based testing applied to the tennis rules.

Sports and games tend to be easy targets for Property Based Testing, because the rules of the game map to properties.

In general, properties are invariants of the domain. Given that certain preconditions are fulfilled, when you exercise the SUT, then certain postconditions should hold.

You may also consider watching my Introduction to Property-based Testing with F# Pluralsight course.