pytest: suggestion: improve raises confusing message when matching

  • a detailed description of the bug or suggestion I would like pytest.raises to check if strings have parenthesis in them when match is not None, so that people have that lightbulb moment, rather than head-scratching when it presents two identical strings as not matching.

    E           AssertionError: Pattern 'Length of values (29) does not match length of index (30)' does not match 'Length of values (29) does not match length of index (30)'
    
  • output of pip list from the virtual environment you are using I believe this is not applicable right now. I’m happy to provide one if you believe other modules or libraries are causing this behavior.

  • pytest and operating system versions Windows 10 64-bit (I know), all updates installed Pytest 5.4.3

  • minimal example if possible edited 22:31 (same day) as the example given was the fixed code including escapes and r prefix

    msg = (
              f"Length of values ({len(newcolumndata)}) "
              f"does not match length of index ({len(data)})"
          )
          with pytest.raises(MismatchedIndexValueError, match=msg):
    

I believe a more helpful error message

=============================================================================== warnings summary =============================================================================== 
tests/whatever/file.py::test_whatever_function_name_is
  file.py:42: UserWarning: non regex passed to match

-- Docs: https://docs.pytest.org/en/latest/warnings.html
=========================================================================== short test summary info ============================================================================

  E           AssertionError: Pattern 'Length of values (29) does not match length of index (30)' does not match 'Length of values (29) does not match length of index (30)'

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (15 by maintainers)

Commits related to this issue

Most upvoted comments

One alternative is to use or to regex match and perform string comparison

I don’t think this is a good idea, because I may want to pass an actual regex which happens to be string-equal but not regex-match.

I think we can improve in these ways:

  1. If string-equal, but not regex-match, show a “Did you mean” help message (as suggested by @RonnyPfannschmidt).
  2. Change “Pattern …” to “Regex pattern …” (as suggested by @asottile).
  3. Prefix r to the regex pattern, as another visual indicator that it’s a regex. (It would need to actually be valid of course).

WDYT?

A check for exact equality + a value error on forgotten regex escape may prevent some headscratching

@Lewiscowles1986 I wasn’t replying to you, sorry if the quote wasn’t clear