I have a function that returns dict based on ResultSet from Beautiful Soup:
return {image["src"]: image["alt"] for image in images_data}
and i just have no idea how to test that. My first idea was preparing a HTML document with images and expected data. Then I just compare expected data to returned dict:
from bs4 import BeautifulSoup
import pytest
@pytest.fixture()
def load_test_data(self):
example_website = (
Path(__file__).parent / "../tests/test_data/example_website.html"
)
with open(example_website, "r") as f:
website_data = BeautifulSoup(f.read(), "html.parser")
return string_operations.prepare_src_and_alt(
website_data.select("img.full-image")
)
expected_data_keys = ("string",)
expected_data_values = ("another_string",)
@pytest.mark.parametrize(
("img_src", "img_alt"), zip(expected_data_keys, expected_data_values)
)
def test_output_should_have_proper_data(self, load_test_data, img_src, img_alt):
data = load_test_data
assert data[img_src] == img_alt
I'm sure that is a bad way to do unit tests. Anyone had an idea how to properly mock ResultSet or make test data?
source https://stackoverflow.com/questions/72578822/python-unit-testing-beautiful-soup-4-resultset-how-to-mock-that
Comments
Post a Comment