Verifying Your Pytest Setup with a Simple Selenium Test
Quick Test to make sure proper installation
In this post, we'll walk through creating a basic Pytest test that uses Selenium WebDriver to navigate to
www.google.com
and validate that the word "Google" appears in the page title. This test serves
as a lightweight check to ensure your Pytest environment is ready to go.
Prerequisites
Before we dive in, make sure you have the following installed:
- Python: Version 3.7 or higher.
- Pytest: Install it using
pip install pytest
orpython3 -m pip install pytest --user
. - Selenium WebDriver: Install it using
pip install selenium
orpython3 -m pip install selenium --user
. - Web Browser Driver: For this example, we'll use
ChromeDriver. Download it from the ChromeDriver website and ensure it matches your Chrome
browser version. Place the
chromedriver
executable in your system’s PATH or specify its location in the code.
Setting Up the Project
- Create a new directory for your project, e.g.,
pytest_setup_test
. - Inside the directory, create a virtual environment (optional but
recommended):
python -m venv venv source venv/bin/activate # On Windows: venvScriptsactivate
- Install the required packages:
pip install pytest selenium
- Create a file named
test_google.py
to hold our test.
Writing the Pytest Test
Here’s the code for test_google.py
, which uses Selenium to navigate to
www.google.com
and checks the page title:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import pytest
# Fixture to set up and tear down the WebDriver
@pytest.fixture
def browser():
# Specify the path to ChromeDriver if not in PATH
# service = Service('/path/to/chromedriver') # Uncomment and update if needed
service = Service()
driver = webdriver.Chrome(service=service)
yield driver
driver.quit()
# Test to validate the page title of google.com
def test_google_title(browser):
browser.get("https://www.google.com")
title = browser.title
assert "Google" in title, f"Expected 'Google' in title, but got '{title}'"
Explanation
- Fixture (
browser
): Thepytest.fixture
decorator defines a reusable setup/teardown function. Here, it initializes a Chrome WebDriver instance and yields it to the test. After the test runs,driver.quit()
closes the browser. - Test Function (
test_google_title
): This function uses thebrowser
fixture to:- Navigate to
https://www.google.com
usingbrowser.get()
. - Retrieve the page title with
browser.title
. - Assert that the word "Google" is in the title. If not, the test fails with a descriptive message.
- Navigate to
- ChromeDriver Path: If ChromeDriver is not in your system’s
PATH, uncomment the
service
line and specify the path to thechromedriver
executable.
Running the Test
To run the test, navigate to your project directory in the terminal and execute:
pytest test_google.py -v
Expected Output
If everything is set up correctly, you should see output similar to this:
============================= test session starts ==============================
platform linux -- Python 3.9.5, pytest-7.4.3, pluggy-1.0.0
collected 1 item
test_google.py::test_google_title PASSED [100%]
============================== 1 passed in 2.34s ===============================
- PASSED: Indicates the test ran successfully, confirming that Pytest and Selenium are configured correctly, and the word "Google" was found in the page title.
- FAILED: If the test fails, check the error message. Common
issues include:
- ChromeDriver not found (verify the path or PATH configuration).
- Network issues preventing access to
www.google.com
. - Mismatched ChromeDriver and Chrome browser versions.
Troubleshooting Tips
- ChromeDriver Issues: Ensure ChromeDriver matches your
Chrome version (check Chrome’s version in
Settings > About Chrome
). Update or reinstall ChromeDriver if needed. - Pytest Not Found: Verify Pytest is installed in your active
environment (
pip show pytest
). - Selenium Errors: Confirm Selenium is installed
(
pip show selenium
) and that you’re using a compatible version with your browser.
﹤li class="list-group-item">Test Fails Due to Title: Occasionally, Google’s title might
vary (e.g., due to localization). You can modify the assertion to be more flexible if needed.
Why This Test?
This test is a simple yet effective way to validate your Pytest setup because it:
- Confirms Pytest is installed and running tests correctly.
- Verifies Selenium WebDriver is configured and can interact with a browser.
- Checks network connectivity and browser compatibility.
- Provides a clear pass/fail outcome with minimal code.
Next Steps
Once this test passes, your Pytest environment is ready! You can expand your test suite to include more complex scenarios, such as:
- Testing form submissions or button clicks.
- Validating other websites or web applications.
- Integrating with CI/CD pipelines for automated testing.
Feel free to tweak this test to suit your needs. For example, you could test a different website or validate other page elements like text or links. Happy testing!