Introduction
In large Selenium automation projects, some setup operations are expensive and do not need to be repeated before every test. For example, loading configuration data, establishing database connections, reading environment settings, or preparing shared test resources only needs to happen once during the entire test execution.
PyTest provides Session Fixtures to handle these situations. A session fixture is created once at the beginning of the PyTest session and remains available until all tests have finished executing.
In this tutorial, you’ll learn what Session Fixtures are, how they work, and how they help improve the performance and maintainability of Selenium automation frameworks.
What are Session Fixtures?
A Session Fixture is a fixture that is created only once during the entire PyTest execution session and is shared across all tests that request it.
Unlike the default function-scoped fixture, a session fixture is initialized only once and reused throughout the complete test run.
Example:
PyTest Session
│
▼
Create Session Fixture
│
┌──────────────┼──────────────┐
│ │ │
▼ ▼ ▼
Test 1 Test 2 Test 3
│ │ │
└──────────────┼──────────────┘
│
▼
Destroy Session Fixture
The same fixture instance is reused by every test during the PyTest session.
Why Use Session Fixtures?
Session Fixtures help you:
Avoid repeating expensive setup operations.
Improve test execution speed.
Share common configuration data.
Reduce duplicate initialization code.
Build efficient automation frameworks.
How Session Fixtures Work
A Session Fixture is created by specifying the scope="session" parameter.
Example:
@pytest.fixture(scope="session")
def playground_url():
return "https://www.testmuai.com/selenium-playground/"
The fixture is created only once and reused by every test that requests it.
Example
import pytest
from selenium import webdriver
# Topic: 40. Fixtures - Session Fixtures
# Practice site: https://www.testmuai.com/selenium-playground/
# Run: pytest -s 40_examples/test_04_session_fixtures.py
#
# Session fixtures run once for the entire pytest session. They are useful for
# expensive setup that should be shared across many tests.
@pytest.fixture(scope="session")
def playground_url():
return "https://www.testmuai.com/selenium-playground/"
def test_session_fixtures(playground_url):
driver = webdriver.Chrome()
try:
driver.get(playground_url)
assert playground_url in driver.current_url
finally:
driver.quit()
Understanding the Code
Import Required Libraries
import pytest
from selenium import webdriver
These modules are required to:
Create PyTest fixtures.
Launch the Chrome browser.
Create a Session Fixture
@pytest.fixture(scope="session")
def playground_url():
The scope="session" parameter tells PyTest to create the fixture only once for the entire test session.
Every test requesting playground_url receives the same fixture instance.
Return the Shared URL
return "https://www.testmuai.com/selenium-playground/"
The fixture returns the Selenium Playground URL.
Instead of hardcoding the URL in multiple tests, every test can reuse this shared value.
Use the Fixture in the Test
def test_session_fixtures(playground_url):
PyTest automatically injects the playground_url fixture into the test function.
There is no need to create or import it manually.
Launch the Browser
driver = webdriver.Chrome()
Starts a new Chrome browser session.
In this example, only the URL is shared. Each test still creates its own browser instance.
Open the Practice Website
driver.get(playground_url)
Uses the shared URL provided by the Session Fixture to open the Selenium Playground website.
Verify the URL
assert playground_url in driver.current_url
The assert statement verifies that the browser has successfully navigated to the expected URL.
If the URL does not match, PyTest marks the test as failed.
Close the Browser
driver.quit()
Closes all browser windows and ends the WebDriver session.
Although the browser is closed after each test, the Session Fixture remains available until the complete PyTest session finishes.
Practical Example
Suppose your Selenium framework stores the application’s base URL in a Session Fixture.
Every test uses the same URL without redefining it, making the framework easier to maintain if the application’s address changes.
Automation Testing Example
Consider an enterprise banking application.
Before executing hundreds of test cases, the automation framework loads:
Environment configuration.
Base application URL.
Test environment settings.
Shared credentials.
Using Session Fixtures allows these resources to be initialized once and reused throughout the entire test suite.
Real-World Example
Session Fixtures are commonly used in:
Selenium automation frameworks
CI/CD pipelines
Regression testing
Cross-browser testing
API automation
Configuration management
Enterprise automation projects
They are especially useful for sharing resources that remain constant throughout the test execution.
Advantages of Session Fixtures
Perform setup only once.
Improve test execution speed.
Share common configuration.
Reduce duplicate initialization code.
Simplify framework maintenance.
Common Mistakes Beginners Make
Using Session Scope for Everything
Not every fixture should use scope="session".
Use Session Fixtures only for resources that can safely be shared across all tests.
Modifying Shared Data
Since every test receives the same fixture instance, avoid modifying shared objects unless necessary.
Unexpected changes may affect other tests.
Confusing Session Fixtures with Browser Fixtures
A Session Fixture does not automatically mean one browser is shared.
In this example, only the URL is shared while each test creates its own browser instance.
Hardcoding Values in Every Test
Instead of repeating configuration values throughout the project, place them inside reusable Session Fixtures.
Best Practices
Use
scope="session"only for shared resources.Store configuration values in Session Fixtures.
Avoid modifying shared fixture data.
Keep Session Fixtures lightweight and reusable.
Use browser fixtures separately when browser isolation is required.
Conclusion
Session Fixtures provide an efficient way to share common resources across an entire PyTest execution session. By creating reusable configuration data and expensive setup operations only once, they improve test execution speed and simplify Selenium automation frameworks. Understanding when to use Session Fixtures helps build scalable, maintainable, and professional test automation projects.
Frequently Asked Questions (FAQs)
What is a Session Fixture?
A Session Fixture is a PyTest fixture that is created once and shared throughout the entire PyTest execution session.
Which scope creates a Session Fixture?
Use:
@pytest.fixture(scope="session")
to create a Session Fixture.
When should I use Session Fixtures?
Use Session Fixtures for resources that should be shared across all tests, such as configuration data, environment settings, or application URLs.
Does a Session Fixture always share one browser?
No.
A Session Fixture can share any reusable resource. In this example, only the application URL is shared, while each test creates its own browser instance.
What happens when the PyTest session ends?
After all tests finish executing, PyTest automatically destroys the Session Fixture and releases any associated resources.
Key Takeaways
Session Fixtures are created once for the entire PyTest session.
Use
scope="session"to define a Session Fixture.Session Fixtures are ideal for sharing configuration and reusable resources.
Avoid modifying shared fixture data during test execution.
Session Fixtures improve execution speed and framework maintainability.
They are widely used in professional Selenium and PyTest automation frameworks.
