QA Graphic

How to Check URL Validity

Quick and Easy

A quick guide to validating URLs after a release using Pytest and Python's requests library.

Introduction

After a software release, ensuring that all URLs are accessible is critical. This blog post demonstrates how to use Pytest and the requests library to check if URLs return a 200 status code, indicating they are valid and accessible. This approach is fast, reliable, and easy to integrate into your testing pipeline.

Prerequisites

  • Python 3.6 or higher
  • Pytest (pip install pytest)
  • Requests library (pip install requests)

Sample Code

Below is a simple Pytest script to check if a URL returns a 200 status code:

import pytest
import requests
def test_url_returns_200():
    url = "https://www.cryan.com"
    try:
        response = requests.get(url, timeout=5)
        assert response.status_code == 200
    except requests.RequestException as e:
        pytest.fail(f"Request failed: {e}")
                

This test sends a GET request to the specified URL and checks if the response status code is 200. If the request fails (e.g., due to a timeout or network issue), the test fails with an error message.

Running the Test

Save the code in a file (e.g., test_urls.py) and run it using the following command:

pytest test_urls.py -v
                

The -v flag provides verbose output, showing the test results in detail.

Scaling to Multiple URLs

To test multiple URLs, you can use Pytest's parameterization feature. Here's an example:

import pytest
import requests
@pytest.mark.parametrize("url", [
    "https://www.cryan.com",
    "https://www.example.com",
    "https://www.python.org",
])
def test_url_returns_200(url):
    try:
        response = requests.get(url, timeout=5)
        assert response.status_code == 200
    except requests.RequestException as e:
        pytest.fail(f"Request failed for {url}: {e}")
                

This script tests multiple URLs in a single test function, making it efficient for checking several endpoints after a release.

Best Practices

  • Set a reasonable timeout (e.g., 5 seconds) to avoid hanging tests.
  • Use parameterization to test multiple URLs efficiently.
  • Integrate tests into your CI/CD pipeline for automated checks post-release.
  • Log failures with detailed messages to aid debugging.

Conclusion

Using Pytest and the requests library, you can quickly validate URLs after a release. This approach is simple, scalable, and integrates well with automated testing workflows. By incorporating these tests into your pipeline, you can ensure your application's URLs remain accessible and reliable.

 

Comments

Add Your Comments

Name:
Comment:

 

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.

Schedule

Wednesday 18 Pytest
Thursday 19 PlayWright
Friday 20 Macintosh
Saturday 21 Internet Tools
Sunday 22 Misc
Monday 23 Media
Tuesday 24 QA