Parametrized Testing

Sending some values through a function and checking the output to make sure it’s correct is a common pattern in software testing. However, calling a function once with one set of values and one check for correctness isn’t enough to fully test most functions. Parametrized testing is a way to send multiple sets of data through the same test and have pytest report if any of the sets failed.

To help understand the problem parametrized testing is trying to solve, let’s take a simple test for add():

 import​ pytest
 import​ tasks
 from​ tasks ​import​ Task
 
 
 def​ test_add_1():
 """tasks.get() using id returned from add() works."""
  task = Task(​'breathe'​, ​'BRIAN'​, True) ...

Get Python Testing with pytest now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.