QA Graphic

Spell-Check Your Site Using Pytest

Use Automation to Check Spelling

Here's a clever way to catch embarrassing spelling mistakes on your website using pytest and spellchecker. This script can be scheduled to run periodically to ensure nothing slips through the cracks!

Why Check for Spelling?

Spelling errors can reduce trust, affect SEO, and just look unprofessional. With this script, you can automatically scan your site's content and get alerted if anything seems off.

Dependencies

  • pytest
  • requests
  • beautifulsoup4
  • pyspellchecker

Install them with:

pip install pytest requests beautifulsoup4 pyspellchecker

The Test Code

Here is the full test you can drop into your test suite:

import pytest
import requests
from bs4 import BeautifulSoup
from spellchecker import SpellChecker
from urllib.parse import urljoin
def get_visible_text(url):
    try:
        response = requests.get(url, timeout=10)
        response.raise_for_status()
    except requests.RequestException as e:
        pytest.fail(f"Failed to fetch {url}: {e}")
    soup = BeautifulSoup(response.text, 'html.parser')
    for element in soup(['script', 'style', 'header', 'footer', 'nav', 'aside']):
        element.decompose()
    text = soup.get_text(separator=' ', strip=True)
    return text
def check_spelling(text, custom_words=None):
    spell = SpellChecker()
    if custom_words:
        spell.word_frequency.load_words(custom_words)
    words = text.split()
    misspelled = spell.unknown(words)
    return misspelled
def test_spelling_cryan_com():
    url = "https://www.cryan.com"
    custom_words = ["cryan", "blog", "tech", "xai"]
    text = get_visible_text(url)
    misspelled_words = check_spelling(text, custom_words=custom_words)
    assert not misspelled_words, (
        f"Spelling errors found on {url}: {misspelled_words}"
    )
if __name__ == "__main__":
    pytest.main(["-v", __file__])

Customization Tips

  • Add more custom words to avoid false positives like brand names or domain-specific terms.
  • Expand to multiple pages by looping through URLs and running the same logic.
  • Integrate with CI/CD for automatic detection during deployment.

 

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

Monday 12 Media
Tuesday 13 QA
Wednesday 14 Pytest
Thursday 15 PlayWright
Friday 16 Macintosh
Saturday 17 Internet Tools
Sunday 18 Misc