Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
pytest Quick Start Guide

You're reading from   pytest Quick Start Guide Write better Python code with simple and maintainable tests

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781789347562
Length 160 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Bruno Oliveira Bruno Oliveira
Author Profile Icon Bruno Oliveira
Bruno Oliveira
Arrow right icon
View More author details
Toc

Scopes

Fixtures are always created when a test function requests them, by declaring them on the parameter list, as we've seen already. By default, each fixture is destroyed when each test finishes.

As mentioned at the beginning of this chapter, some fixtures can be costly to create or set up, and it would be helpful to be able to create as few instances of it as possible, to save time. Here are some examples:

  • Initializing database tables
  • Reading cached data from a disk, for example, large CSV data
  • Starting up external services

To help solve this issue, fixtures in pytest can have different scopes. The scope of a fixture defines when the fixture should be cleaned up. While the fixture is not cleaned up, tests requesting the fixture will receive the same fixture value.

The scope parameter of the @pytest.fixture decorator is used to set the fixture's scope:

@pytest.fixture...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at R$50/month. Cancel anytime