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
assertin unit tests. - Write unit tests for both modules of PA1.
Announcements¶
- Read: Week 8 (due Oct 14)
- Start: Project 1 (due Oct 21)
- Part A due Tuesday, Oct 14
- Did everyone add the new textbook? (see Wednesday)
Pytest Tutorial¶
[40 min]
- If you are absent today, complete this tutorial on your own
- Be sure to submit your
test_triangles.pyto 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)