Better Programming

Advice for programmers.

Follow publication

Member-only story

Understand 5 Scopes of Pytest Fixtures

Efficiently run your test code in Python

Xiaoxu Gao
Better Programming
Published in
5 min readJun 24, 2020

Photo by David Travis on Unsplash

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.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Xiaoxu Gao
Xiaoxu Gao

Written by Xiaoxu Gao

I’m a Developer with a focus on Python and Data Engineering. I write stuff to talk to myself and the world. You can find me on linkedin.com/in/xiaoxugao/.

Responses (9)

Write a response

Really appreciate the post

Thanks for the post ! Really helpful.

This article was so much helpful.Thanks