Add function and class descriptions to tests (#2715)

Co-authored-by: Reinier van der Leer <github@pwuts.nl>
This commit is contained in:
Andres Caicedo
2023-04-24 14:55:49 +02:00
committed by GitHub
parent 40a75c804c
commit f8dfedf1c6
19 changed files with 147 additions and 67 deletions

View File

@@ -42,8 +42,8 @@ Additional aspects:
class TestScrapeText:
# Tests that scrape_text() returns the expected text when given a valid URL.
def test_scrape_text_with_valid_url(self, mocker):
"""Tests that scrape_text() returns the expected text when given a valid URL."""
# Mock the requests.get() method to return a response with expected text
expected_text = "This is some sample text"
mock_response = mocker.Mock()
@@ -59,14 +59,13 @@ class TestScrapeText:
url = "http://www.example.com"
assert scrape_text(url) == expected_text
# Tests that an error is raised when an invalid url is provided.
def test_invalid_url(self):
"""Tests that an error is raised when an invalid url is provided."""
url = "invalidurl.com"
pytest.raises(ValueError, scrape_text, url)
# Tests that the function returns an error message when an unreachable
# url is provided.
def test_unreachable_url(self, mocker):
"""Test that scrape_text returns an error message when an invalid or unreachable url is provided."""
# Mock the requests.get() method to raise an exception
mocker.patch(
"requests.Session.get", side_effect=requests.exceptions.RequestException
@@ -78,9 +77,8 @@ class TestScrapeText:
error_message = scrape_text(url)
assert "Error:" in error_message
# Tests that the function returns an empty string when the html page contains no
# text to be scraped.
def test_no_text(self, mocker):
"""Test that scrape_text returns an empty string when the html page contains no text to be scraped."""
# Mock the requests.get() method to return a response with no text
mock_response = mocker.Mock()
mock_response.status_code = 200
@@ -91,9 +89,8 @@ class TestScrapeText:
url = "http://www.example.com"
assert scrape_text(url) == ""
# Tests that the function returns an error message when the response status code is
# an http error (>=400).
def test_http_error(self, mocker):
"""Test that scrape_text returns an error message when the response status code is an http error (>=400)."""
# Mock the requests.get() method to return a response with a 404 status code
mocker.patch("requests.Session.get", return_value=mocker.Mock(status_code=404))
@@ -103,8 +100,8 @@ class TestScrapeText:
# Check that the function returns an error message
assert result == "Error: HTTP 404 error"
# Tests that scrape_text() properly handles HTML tags.
def test_scrape_text_with_html_tags(self, mocker):
"""Test that scrape_text() properly handles HTML tags."""
# Create a mock response object with HTML containing tags
html = "<html><body><p>This is <b>bold</b> text.</p></body></html>"
mock_response = mocker.Mock()