Skip to content

Oct 10: 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

Pytest Tutorial

[40 min]

Installing/Running pytest

  • If you are absent today, complete this tutorial on your own
  • Be sure to submit your test_triangles.py to Gradescope

Note

Docstrings are not required for test functions. But you should still write a module docstring.

More Examples

[10 min]

Example tests for HW6.1

An assert statement is used to see if each function call returns the expected tuple.

from division import divide

def test_divide_0_by_10():
    assert divide(0, 10) == (0, 0)

def test_divide_134_by_7():
    assert divide(134, 7) == (19, 1)

def test_divide_56_by_57():
    assert divide(56, 57) == (0, 56)

def test_divide_56_by_16():
    assert divide(56, 16) == (3, 8)