Testing your code is a good idea because it usually leads to great improvements. There's many libraries in python that will help with testing but a favourable on called pytest. In this series of videos we demonstrate how the use it.
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
from blackjack import card_score def test_simple_usecase(): card_score("JK") == 20
You can start the first test by calling;
Feedback? See an issue? Something unclear? Feel free to mention it here.
If you want to be kept up to date, consider getting the newsletter.