Skip to Main Content
Python Testing with pytest
book

Python Testing with pytest

by Brian Okken
February 2022
Intermediate to advanced content levelIntermediate to advanced
274 pages
6h 28m
English
Pragmatic Bookshelf
Content preview from Python Testing with pytest

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 ==========================
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python Testing with pytest

Python Testing with pytest

Brian Okken

Publisher Resources

ISBN: 9781680509427Errata Page