Python unit testing Beautiful Soup 4 - ResultSet. How to mock that?

Viewed 76

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?

1 Answers

From the source code, ResultSet is just a list with an additional attribute. Your code might not even use that "source" attribute: Just set data to a list with the expected contents. If you don't know what those contents look like, run BeautifulSoup against a dummy webpage like you show in the code, and print the ResultSet.

If you need the source, you can try setting that to a mock object of the appropriate type.

Related