Skip to content

Multiple image comparison extension types in one test? #687

@drjasonharrison-vp-eio

Description

@drjasonharrison-vp-eio

I'm starting with the example test_custom_image_extension ( https://github.com/tophat/syrupy/blob/main/tests/examples/test_custom_image_extension.py ) and wondering how to extend this to multiple image formats. For example JPEG and PNG.

from syrupy.extensions.single_file import SingleFileSnapshotExtension

class JPEGImageExtension(SingleFileSnapshotExtension):
    @property
    def _file_extension(self) -> str:
        return "jpg"

class PNGImageExtension(SingleFileSnapshotExtension):
    @property
    def _file_extension(self) -> str:
        return "png"

@pytest.fixture
def snapshot_jpeg(snapshot):
    return snapshot.use_extension(JPEGImageExtension)

@pytest.fixture
def snapshot_png(snapshot):
    return snapshot.use_extension(PNGImageExtension)

and then pass both fixtures into the test:

def create_image(format):
   if format == 0:
        return "JPEG_IMAGE_DATA", "jpeg_image.jpg"
   
    return "PNG_IMAGE_DATA", "png_image.png"

def test_image_format(snapshot, snapshot_jpeg, snapshot_png):
    image_data, filename = create_image(0)
    filename_extension = filename.split(".")[-1]
    assert image_data == snapshot_jpeg(name=filename)

    image_data, filename = create_image(1)
    filename_extension = filename.split(".")[-1]
    assert image_data == snapshot_png(name=filename)

I'm not sure if a snapshot extension can take additional parameters, like the file extension (.jpg, .png) and select the inherited class of SingleFileSnapshotExtension. This would allow us to do something like:

def test_image_format(snapshot, snapshot_image):
    image_data, filename = create_image(0)
    filename_extension = filename.split(".")[-1]
    assert image_data == snapshot_image(name=filename, image_type=filename_extension)

    image_data, filename = create_image(1)
    filename_extension = filename.split(".")[-1]
    assert image_data == snapshot_image(name=filename, image_type=filename_extension)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions