Combining Markers with Fixtures

Markers can be used in conjunction with fixtures. They also can be used in conjunction with plugins and hook functions (but that’s a topic for Chapter 15, Building Plugins). Here, we’ll combine markers and fixtures to help test the Cards application.

The builtin markers took parameters, while the custom ones we’ve used so far do not. Let’s create a new marker called num_cards that we can pass to the cards_db fixture.

The cards_db fixture currently cleans out the database for each test that wants to use it:

 @pytest.fixture(scope=​"function"​)
 def​ ​cards_db​(session_cards_db):
  db = session_cards_db
  db.delete_all()
 return​ db

If we want to, say, have three cards in the ...

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.