August 2018
Beginner
160 pages
4h 8m
English
To show scopes in action, let's take a look at a common pattern that's used when your tests involve some form of database. In the upcoming example, don't focus on the database API (which is made up anyway), but on the concepts and design of the fixtures involved.
Usually, the connection to a database and table creation are slow. If the database supports transactions, which is the ability to perform a set of changes that can be applied or discarded atomically, then the following pattern can be used.
For starters, we can use a session-scoped fixture to connect and initialize the database with the tables we need:
@pytest.fixture(scope="session")def db(): db = connect_to_db("localhost", "test") db.create_table(Series) db.create_table(Actors) ...Read now
Unlock full access