0
0
PyTesttesting~5 mins

Fixture scope (function, class, module, session) in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the 'function' scope mean in pytest fixtures?
The fixture is created and destroyed for each test function that uses it. It runs fresh for every test.
Click to reveal answer
beginner
Explain the 'class' scope for pytest fixtures.
The fixture is created once per test class and shared among all test methods in that class.
Click to reveal answer
intermediate
What happens when a fixture has 'module' scope?
The fixture is created once per module (file) and shared by all tests in that module.
Click to reveal answer
intermediate
Describe the 'session' scope in pytest fixtures.
The fixture is created once for the entire test session and shared by all tests across all modules.
Click to reveal answer
intermediate
Why would you use a 'session' scoped fixture instead of a 'function' scoped one?
To save time by setting up expensive resources only once for all tests, like a database connection.
Click to reveal answer
Which pytest fixture scope creates a new fixture instance for each test function?
Afunction
Bclass
Cmodule
Dsession
If you want a fixture to be shared by all tests in a single Python file, which scope should you use?
Afunction
Bclass
Cmodule
Dsession
What is the scope that shares a fixture across all test classes and functions in the entire test run?
Asession
Bfunction
Cmodule
Dclass
Which scope would be best for a fixture that sets up a database connection used by many tests?
Afunction
Bsession
Cclass
Dmodule
A fixture with 'class' scope is created how many times per test class?
AOnce per session
BOnce per test function
COnce per module
DOnce per test class
Describe the differences between the four pytest fixture scopes: function, class, module, and session.
Think about how often the fixture is created and shared.
You got /4 concepts.
    Explain a real-life scenario where using a 'session' scoped fixture is better than a 'function' scoped fixture.
    Imagine setting up something costly once instead of many times.
    You got /3 concepts.