Skip to content

Oct 16: Unit Testing with pytest

Learning Objectives

After today's class, you should be able to:

  • Install and run pytest on your computer.
  • Explain the role of assert in unit tests.
  • Write unit tests for both modules of PA1.

Announcements

  • Mid-Eval survey – LAST CALL!
    • Worth 5 pts like everything else
  • Ch 8 on zyBooks – due Tuesday
  • PA 1 Poker Dice (modules + testing)
    • Readiness Quiz now available
    • Part A due Tuesday 10/24 (30 pts)
    • Part B due Tuesday 10/31 (70 pts)

Pytest Tutorial

[30 min]

  • Installing/Running pytest
    • If you are absent today, complete this tutorial on your own
    • Bring your completed test_triangles.py to office hours

PA1 Unit Tests

[20 min]

Here is a starting point for Part A of the project. Two example assert statements are provided for each test function. You should add at least three more to each function, so that every requirement of the project is tested.

Note: Docstrings are generally not required for test functions. But you should still have a module docstring with your name and date.

test_dice.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""Test the dice module.

Name: YOUR NAME
Date: THE DATE
"""

from dice import roll_dice
from dice import are_valid
from dice import add_values
from dice import num_faces


def test_roll_dice():
    assert roll_dice(5) == ['Q', 'Q', '9', 'J', 'K']
    assert roll_dice(0, 1) == ['9']


def test_are_valid():
    assert are_valid(['A', 'K'])
    assert not are_valid(['1', '2', '3'])


def test_add_values():
    assert add_values(['10', '10', '10', '10']) == 40
    assert add_values([]) == -1


def test_num_faces():
    assert num_faces(['Q', '9', 'Q', '9'], 'Q') == 2
    assert num_faces(None, None) == -1

On Part B, you will need to define a test function for each of the nine categories. Each test function should have at least five assert statements. (The autograder has over ten assert statements in each category!) Don't just add assert statements for the sake of having five or more. Make sure you test each situation required by the project.

Note: The following code abbreviates score_dice as sd, rather than import every constant.

test_score_dice.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
"""Test the score_dice module.

Name: YOUR NAME
Date: THE DATE
"""

import score_dice as sd


def test_one_pair():
    assert sd.calculate_score(['9', '9', '10', 'J', 'K'], sd.PAIR) == 18
    assert sd.calculate_score(['9', '10', 'J', 'Q', 'K'], sd.PAIR) == 0