Member-only story
Understand 5 Scopes of Pytest Fixtures
Efficiently run your test code in Python
There are three mainstream unit test frameworks for Python: the built-in unittest
, nosetests
and pytest
. According to many unofficial rankings on the internet, Pytest
is the most popular test framework thanks to its minimal amount of boilerplate, rich features, and a growing number of plugins.
A test fixture is a concept used in both electronics and software. It’s a piece of software or device that sets up a system to satisfy certain preconditions of the process. Its biggest advantage is that it provides consistent results so that the test results can be repeatable. Examples of fixtures could be loading test set to the database, reading a configuration file, setting up environment variables, etc.
What’s a Fixture in Pytest?
Fixture plays a quite important role in Pytest. Compared to the classic xUnit style where you normally have a setup()
and teardown()
function, the fixture in Pytest is more flexible and flat. Each fixture is defined using the @pytest.fixture
decorator on top of a function and this function will do the job like reading a configuration file.