
Parametrization in Pytest
Use the same code over and over
Parametrization in Pytest allows you to run the same test function multiple times with different inputs. Instead of writing separate test functions for each set of data, you can define a single test and provide various argument sets using the @pytest.mark.parametrize decorator. This approach is especially useful for testing functions that need to handle a variety of inputs, edge cases, or data types.
Why Use Parametrization?
- Code Reusability: Write one test function and reuse it for multiple test cases.
- Efficiency: Reduce boilerplate code and make your test suite easier to maintain.
- Clarity: Clearly define the inputs and expected outputs for each test case.
- Comprehensive Testing: Easily test a wide range of scenarios without extra effort.
Code Example
This code will check to see if various internal sites are up and running. I ran similar code in the past. This was done so that I could see if there are any issues before the morning standup.
If I didn't use parametrization here, there would be multiple test cases which could cause overhead issues if changes needed to be done.
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.common.exceptions import WebDriverException
# List of websites to test
WEBSITES = [
"https://www.company.com",
"https://qa1.company.com",
"https://qa2.company.com",
"https://stage.company.com"
]
@pytest.fixture
def chrome_driver():
"""Fixture to set up and tear down Chrome WebDriver"""
# Set up Chrome options
chrome_options = Options()
chrome_options.add_argument("--headless") # Run in headless mode
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
# Initialize driver
driver = webdriver.Chrome(options=chrome_options)
driver.set_page_load_timeout(30) # Set timeout to 30 seconds
yield driver
# Teardown
driver.quit()
@pytest.mark.parametrize("website", WEBSITES)
def test_website_is_up(chrome_driver, website):
"""
Test if a website loads successfully by checking:
1. Page loads without timeout
2. HTTP status is 200 (implicitly checked via successful load)
3. Page title is not empty
"""
try:
# Attempt to load the website
chrome_driver.get(website)
# Check if page title exists and is not empty
title = chrome_driver.title
assert title, f"Website {website} loaded but has no title"
# Optional: Check if body element exists
body = chrome_driver.find_element(By.TAG_NAME, "body")
assert body is not None, f"Website {website} has no body content"
print(f"? {website} is up and running (Title: {title})")
except WebDriverException as e:
pytest.fail(f"Website {website} failed to load: {str(e)}")
except AssertionError as e:
pytest.fail(f"Website {website} loaded but content check failed: {str(e)}")
if __name__ == "__main__":
pytest.main(["-v"])
Permalink
mocker.spy
Learn how to use Pytest's mocker.spy for robust website testing
Testing interactions with external services or complex internal functions can be tricky. You want to ensure your website behaves correctly without relying on the actual implementation, which might be slow, unreliable, or have side effects. That's where pytest-mock's spy comes in!
What is mocker.spy?
mocker.spy lets you wrap any callable (function, method, etc.) and record its calls. You can then assert how many times it was called, what arguments it received, and what values it returned. This is incredibly useful for verifying interactions without actually mocking the underlying implementation.
Why is it cool for website testing?
Imagine you have a website that:
- Logs user activity to an external analytics service.
- Sends emails for password resets.
- Interacts with a third-party API for data retrieval.
Instead of actually sending data to analytics, sending real emails, or hitting the live API, you can use mocker.spy to verify that these interactions occurred as expected.
A Practical Example: Tracking Analytics Events
Let's say your website has a function that logs user interactions to an analytics service:
# website/analytics.py
import requests
def track_event(user_id, event_name, event_data):
try:
requests.post("https://analytics.example.com/track", json={
"user_id": user_id,
"event_name": event_name,
"event_data": event_data,
})
except requests.exceptions.RequestException as e:
print(f"Error tracking event: {e}")
And your website's view function calls this:
# website/views.py
from website.analytics import track_event
def process_user_action(user_id, action_data):
# ... process user action ...
track_event(user_id, "user_action", action_data)
# ... more logic ...
Here's how you can test it with mocker.spy:
# tests/test_views.py
from website.views import process_user_action
from website.analytics import track_event
def test_process_user_action_tracks_event(mocker):
spy = mocker.spy(track_event)
user_id = 123
action_data = {"item_id": 456}
process_user_action(user_id, action_data)
spy.assert_called_once_with(user_id, "user_action", action_data)
By incorporating mocker.spy into your website testing strategy, you can create robust and reliable tests that give you confidence in your website's functionality. Happy testing!
PermalinkNaming Screenshots Dynamically in Pytest
Automate Screenshot Naming in Pytest: Easily Identify Test Failures
When running UI tests, capturing screenshots can be an invaluable debugging tool. However, managing these screenshots can quickly become chaotic if they are not properly labeled. One effective way to make screenshots easier to organize and track is by incorporating the test name into the filename. This ensures that each screenshot can be traced back to the exact test that generated it.
Capturing the Current Test Name in Pytest
Pytest provides an environment variable called PYTEST_CURRENT_TEST
, which contains information about the currently executing test. We can extract the test name from this variable and use it to generate meaningful screenshot filenames.
Here's an example of how to do this in a Selenium-based test:
import os
import time
from datetime import datetime
def test_full_page_screenshot_adv(browser):
browser.set_window_size(1315, 2330)
browser.get("https://www.cryan.com") # Navigate to the test page
# Extract the current test name
mytestname = os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0]
# Create a timestamp for unique filenames
log_date = datetime.now().strftime('%Y-%m-%d-%H-%M')
# Define the screenshot path
screenshot_path = f"{mytestname}-{log_date}.png"
# Capture and save the screenshot
browser.save_screenshot(screenshot_path)
print(f"Screenshot saved as {screenshot_path}")
How It Works
- Retrieve the Current Test Name:
- The environment variable
PYTEST_CURRENT_TEST
holds information about the currently running test.
- Using
.split(':')[-1]
, we extract the actual test name from the full test path.
- Further splitting by spaces (
split(' ')[0]
) ensures we only get the function name.
- The environment variable
- Generate a Timestamp:
- The
datetime.now().strftime('%Y-%m-%d-%H-%M')
function creates a timestamp in the formatYYYY-MM-DD-HH-MM
to ensure unique filenames.
- The
- Save the Screenshot:
- The test name and timestamp are combined to form a filename.
- The screenshot is saved using Selenium's
save_screenshot()
method.
- The test name and timestamp are combined to form a filename.
Why This Matters
- Easier Debugging: Knowing which test generated a screenshot makes debugging test failures much simpler.
- Organized Test Artifacts: Each screenshot is uniquely named, reducing the chances of overwriting files.
- Automated Report Integration: The structured filenames can be linked to test reports, making them more informative.
Final Thoughts
By incorporating the test name into the screenshot filename, you can quickly identify which test generated a particular screenshot. This small tweak can save time when reviewing test results, especially in large automation suites.
Try implementing this in your test framework and see how much easier it becomes to manage your UI test screenshots!
PermalinkPyTest Install
Alternative way to install PyTest
The popular pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.
Python is a good language to learn. According to the TIOBE and PYPL index, Python is the top programing language with C and Java closely behind.
If your application is Python base, QA Engineers may find that writing automation in Python may lead to a better understanding of the code logic - which in turn will result into better testing.
If your installing PyTest on your local MacBook Pro at work, you may run into issues with permissions. The local IT department may have your computer locked down and some of the installs will require Administrator permissions.
Here are the install instructions if you have limited rights.
PyTest User Install
Use Python3 -m pip to install PyTest and Selenium:
python3 -m pip install pytest --user
python3 -m pip install selenium --user
Sample Test File
Here's a simple code sample to validate the install worked:
#!/usr/bin/env /usr/bin/python3 # Verify the Pytest was installed import pytest from selenium import webdriver import sys import requests from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from webdriver_manager.chrome import ChromeDriverManager from time import sleep def test_google(): global chrome_driver chrome_driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) chrome_driver.get('https://www.google.com/') title = "Google" assert title == chrome_driver.title pageSource = chrome_driver.page_source gateway = "Carbon neutral since 2007" if not gateway in pageSource: pytest.fail( "Google Changed their Webpage") chrome_driver.close()
Save this file on your computer, I would recommend saving it in ~/pytest/google.py
Execute Test
To execute this test, open up Terminal:
cd ~/pytest/ pytest --no-header -v google.pyPermalink
About
Welcome to Pytest Tips and Tricks, your go-to resource for mastering the art of testing with Pytest! Whether you're a seasoned developer or just dipping your toes into the world of Python testing, this blog is designed to help you unlock the full potential of Pytest - one of the most powerful and flexible testing frameworks out there. Here, I'll share a treasure trove of practical insights, clever techniques, and time-saving shortcuts that I've gathered from years of writing tests and debugging code.
Check out all the blog posts.
Blog Schedule
Friday 21 | Macintosh |
Saturday 22 | Internet Tools |
Sunday 23 | Misc |
Monday 24 | Media |
Tuesday 25 | QA |
Wednesday 26 | Pytest |
Thursday 27 | PlayWright |