syrusakbary / snapshottest

Snapshot Testing utils for Python 📸
MIT License
525 stars 102 forks source link

Non to-string version on dataclasses #160

Open C0DK opened 3 years ago

C0DK commented 3 years ago

When utilizing dataclasses you can define them to be identified by it's attributes. It'd be nice if rather than doing the "GeneralRepr" solution, you would simply use the dir(obj) when making a snapshot of a given dataclass, rather than simply using the __repr__ version. It also makes it a whole lot more difficult to check the diffs when looking at two lists of dataclasses.

In general i wish your lovely module had some custom way of doing the snapshot representation of a given class, if predefined.

i.e a config where you could create a function like:


def to_snapshot(obj: object):
  if isinstance(obj, Person):
    return {obj.name, obj.age}
  return super().to_snapshot

Something..

So it'd be possible to improve the snapshots a bit without having to do a whole lot of bootstrapping for each individual test and/or override __repr__ to return a non-string, which i think is bad practice.