Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give custom name to image snapshot file #555

Closed
obendidi opened this issue Oct 7, 2021 · 7 comments · Fixed by #563
Closed

Give custom name to image snapshot file #555

obendidi opened this issue Oct 7, 2021 · 7 comments · Fixed by #563
Labels
feature request New feature or request released

Comments

@obendidi
Copy link
Contributor

obendidi commented Oct 7, 2021

Hello I have some tests where I need to generate multiple snapshot images in the same test.

Is it possible to provide the target snapshot name file to snapshot when asserting the images are equal ?

My use case:

from syrupy.extensions.image import PNGImageSnapshotExtension
import base64
import pytest


@pytest.fixture
def snapshot(snapshot):
    return snapshot.use_extension(PNGImageSnapshotExtension)

def test_some_func(snasphot):
   base64_img1, base64_img2 = func()

    assert base64.base64decode(base64_img1) == snapshot
    assert base64.base64decode(base64_img2) == snapshot

It works as intended, but IMO the syntax is weird and magical, (we are basically testing 2 different things against the same object).

what I'm looking for / propose is to be able to set a custom filename for the snapshot if we want:

    assert base64.base64decode(base64_img1) == snapshot(filename="photo_image.png")
    assert base64.base64decode(base64_img2) == snapshot(filename="signature_image.png")

Thanks!

@noahnu noahnu added the feature request New feature or request label Oct 7, 2021
@noahnu
Copy link
Collaborator

noahnu commented Oct 7, 2021

Not possible at the moment, and there are some assumptions about filename for the PNG extension, however perhaps we can add some "name" field.

@obendidi
Copy link
Contributor Author

obendidi commented Oct 7, 2021

If i understood correctly the lib currently uses the test function name as name for the png + the number of calls to snapshot as a suffix, to have something like this:

__snapshots__/<test_file_name>/<test_function_name>.<num_call_if_>_0>.png

can we perhaps add prefix_name that overides, the num_call that is added automatically ?

would be happy to contribute in any case ^^

@noahnu
Copy link
Collaborator

noahnu commented Oct 7, 2021

That logic happens here: https://github.com/tophat/syrupy/blob/e008935c052106d157196ca77415f4773a14f64a/src/syrupy/extensions/base.py#L79. You should be able to adjust it for a custom extension or for the PNG / single file extension. There might be assumptions around it in terms of discovering unused snapshots. Feel free to explore it, contributions welcome. This bit gets a bit tricky though.

@danfrankj
Copy link

+1 for this feature. We have string string assertions that need to land in separate named files. As soon as that lands, we'll happily migrate off of snapshottest

@noahnu
Copy link
Collaborator

noahnu commented Oct 21, 2021

@danfrankj would the behaviour of overriding num_call as @Bendidi suggested be sufficient? I don't think we could easily support full control of the name (outside of a custom extension) due to challenges with snapshot discovery.

noahnu pushed a commit that referenced this issue Nov 3, 2021
The snapshot_name_suffix will hold an optional suffix to be used instead
of index in the snapshot_name. The suffix will by default be formatted to
be between brackets: "[<snapshot_name_suffix>]"

Co-authored-by: Ouail Bendidi <ouail.bendidi@gmail.com>
tophat-opensource-bot pushed a commit that referenced this issue Nov 3, 2021
# [1.5.0](v1.4.7...v1.5.0) (2021-11-03)

### Features

* add support for custom snapshot names, close [#555](#555) ([#563](#563)) ([81a8a45](81a8a45))
@tophat-opensource-bot
Copy link
Contributor

🎉 This issue has been resolved in version 1.5.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@noahnu
Copy link
Collaborator

noahnu commented Nov 3, 2021

@iamogbz Looks like this github comment was posted by the dry run for the next branch ^.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request released
Projects
None yet
4 participants