Creating Custom Identifiers

You can define a function to generate identifiers by using the ids parameter. Often the builtin str or repr functions work fine.

Let’s try using str as an ID function:

 card_list = [
  Card(​"foo"​, state=​"todo"​),
  Card(​"foo"​, state=​"in prog"​),
  Card(​"foo"​, state=​"done"​),
 ]
 
»@pytest.mark.parametrize(​"starting_card"​, card_list, ids=str)
 def​ ​test_id_str​(cards_db, starting_card):
  ...

Here we added ids=str. We also moved the list of cards to a named variable to allow shorter code samples in the rest of this section.

Here’s what our node IDs look like now:

 $ ​​pytest​​ ​​-v​​ ​​test_ids.py::test_id_str
 ========================= test session starts ==========================

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.