Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

What actually is a pytest fixture?

+2
−1

Pytest uses fixtures. Their docs explain what fixtures do: https://docs.pytest.org/en/stable/explanation/fixtures.html

But what actually is a fixture? Is there a definition for it? Is this a term from computer science, or something pytest invented?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

A definition is refrerred to on that page (1 comment)

1 answer

+4
−0

The term long predates Pytest and is not at all specific to Python. The idea is described on Wikipedia:

In the context of software a test fixture (also called "test context") is used to set up system state and input data needed for test execution.[2][3] For example, the Ruby on Rails web framework uses YAML to initialize a database with known parameters before running a test.[4] This allows for tests to be repeatable, which is one of the key features of an effective test framework.[2]

Essentially, it's some object that the test framework (Pytest in this case) creates automatically to represent the environment in which the test code will run - a dependency which is injected into the code, similar to a "context" variable passed to a drawing function in JavaScript.

In the Pytest implementation, the dependency injection takes the form of automatically passing that object as a parameter to the test code; when Pytest analyzes a test module to "discover" tests, it also dynamically sets up the necessary logic to construct and pass that value, along with whatever else might be required (such as parameter values for a parametrized test).

There are simpler, if less automatic, ways to do this sort of thing, too. For example, one could simply set up a global variable (such that the calling code finds it in the enclosing global scope), and use a context manager to repeatedly initialize the environment, call a test function and clean up again.

The term is used by analogy from electronic and other physical devices: in these cases, the fixture physically fixes the device in place for testing, to ensure consistent and reproducible test conditions.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

An example: a test database. The fixture may create a test database, delete it when the test finishes... (1 comment)

Sign up to answer this question »