镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Enhancement: Present AssertionError differently #183

Open

Description

@stdedos

It might be just me, but would you consider a different output for failed tests?

data.folders[]: Expected 1 values but got 2
java.lang.AssertionError: data.folders[]: Expected 1 values but got 2

Of course, having the expectedStr makes this error somewhat more readable.

However, even in this case, an output like the one I'm proposing will be better in my opinion (you will be able to see what values were received).

My proposal is tl;dr JSON is pretty printed, sorted, and diffed:

pytest-dev/pytest#11571

...

Now:

- {
+ OrderedDict({
      'cell': '(056)-022-8631',
      'dob': {
-         'age': 34,
?                ^
+         'age': 44,
?                ^
-         'date': '1953-11-04T01:21:04Z',
?                    ^              ^
+         'date': '1983-11-04T01:21:14Z',
?                    ^              ^
      },
      'email': 'zeyneb.elfring@example.com',
      'gender': 'female',
      'id': {
          'name': 'BSN',
          'value': '36180866',
      },
      'location': {
-         'city': 'Tholen',
?                  ^
+         'city': 'tholen',
?                  ^
          'coordinates': {
              'latitude': '46.8823',
              'longitude': '175.8856',
          },
-         'postcode': 64509,
?                         ^
+         'postcode': 64504,
?                         ^
          'state': 'groningen',
          'street': '2074 adriaen van ostadelaan',
          'timezone': {
              'description': 'Adelaide, Darwin',
              'offset': '+9:30',
          },
      },
      'login': {
          'md5': 'bafe8cf9d37806a7b13edc218d5ff762',
          'password': 'ontario',
          'salt': 'QVBKgEjy',
          'sha1': 'cacef09ff61072d1c55732963766fa84e919aa7a',
          'sha256': 'cc86af47aedbdbb1de73ff10484996fe9785c47c0fc191b7c67eaf71e0782300',
          'username': 'smallgorilla897',
          'uuid': '37e30c59-bc79-4172-aac6-e2c640e165fa',
      },
      'name': {
-         'first': 'Zeyneb',
?                   ^
+         'first': 'zeyneb',
?                   ^
-         'last': 'Elfring',
?                  ^
+         'last': 'elfring',
?                  ^
          'title': 'mrs',
      },
      'nat': 'NL',
      'phone': '(209)-143-9697',
      'picture': {
          'large': 'https://randomuser.me/api/portraits/women/37.jpg',
          'medium': 'https://randomuser.me/api/portraits/med/women/37.jpg',
          'thumbnail': 'https://randomuser.me/api/portraits/thumb/women/37.jpg',
      },
      'registered': {
          'age': 3,
          'date': '2014-12-07T06:54:14Z',
      },
- }
+ })

Activity

  1. carterpage commented on Jul 26, 2024

    @carterpage
    Member

    This looks pretty cool. It seems programmatically a big lift to add to the library. Any ideas how to do this without deep modifications? Possibly need an option for minimal "location" metadata to the existing messages and then this pretty print would be a separate function abstracted from the core code. It would need a ton of test cases, since this could get buggy. Need to think about how to handle really large JSON messages with just a few errors.

    I'll leave this open in case people have some ideas.

  2. stdedos commented on Jul 27, 2024

    @stdedos
    Author

    It seems programmatically a big lift to add to the library. Any ideas how to do this without deep modifications?

    You can start smaller with a "dumb" unified-diff presentation?

    I'd say also "and/or add a lambda functionality to the error message (like https://junit.org/junit5/docs/current/user-guide/#writing-tests-assertions)", but I do not have solid way of how would this help

  3. javiertuya commented on Oct 30, 2024

    @javiertuya

    I use https://github.com/javiertuya/visual-assert to assert (and soft assert) differences in strings or files. This can save the diffs in html files for further inspection and display the differences in the development environment. To display JSON differences using this approach it would require to pretty-print and sort keys. Maybe this could be an optional step after JSONassert has detected that there are differences

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions