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.