Parametrizing Functions

To parametrize a test function, add parameters to the test definition and use the @pytest.mark.parametrize() decorator to define the sets of arguments to pass to the test, like this:

 import​ ​pytest
 from​ ​cards​ ​import​ Card
 
 
 @pytest.mark.parametrize(
 "start_summary, start_state"​,
  [
  (​"write a book"​, ​"done"​),
  (​"second edition"​, ​"in prog"​),
  (​"create a course"​, ​"todo"​),
  ],
 )
 def​ ​test_finish​(cards_db, start_summary, start_state):
  initial_card = Card(summary=start_summary, state=start_state)
  index = cards_db.add_card(initial_card)
 
  cards_db.finish(index)
 
  card = cards_db.get_card(index)
 assert​ card.state == ​"done"

The test_finish() function now ...

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.