Remember to first install pytest (best done via a virtualenv).
python -m pip install pytest
Note that this is the code (in blackjack.py
) that we will test.
def card_score(cards):
numbers = [c for c in cards if c in "23456789"]
faces = [c for c in cards if c in "JQK"]
n_aces = sum([1 for c in cards if c == "A"])
score = sum([int(i) for i in numbers]) + len(faces) * 10
while (score + n_aces * 11) > 21:
score += 1
n_aces -= 1
return score if score < 21 else 0
Here's the first test that we wrote (in test_blackjack.py
).
from blackjack import card_score
def test_simple_usecase():
card_score("JK") == 20
You can start the first test by calling;
pytest test_blackjack.py