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

Ignore whitespace differences when testing error messages #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jdayton3
Copy link

Problem

When I run the unit tests locally, I get failures for test_bin.py's TestError tests. It looks to be because my detected terminal width is configured differently than is expected, so the "usage" portion of the error message wraps differently than expected:

py37: commands[0]> coverage run --source=genson -m unittest
.......................................FFF.............................................................
======================================================================
FAIL: test_no_input (test.test_bin.TestError)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\repos\GenSON\test\test_bin.py", line 81, in test_no_input
    'noting to do - no schemas or objects given'))
AssertionError: 'usag[59 chars]CES]\r\n              [-s SCHEMA] [-$ SCHEMA_U[85 chars]\r\n' != 'usag[59 chars]CES]\n              [-s SCHEMA] [-$ SCHEMA_URI[77 chars]en\n'
- usage: genson [-h] [--version] [-d DELIM] [-e ENCODING] [-i SPACES]
?                                                                    -
+ usage: genson [-h] [--version] [-d DELIM] [-e ENCODING] [-i SPACES]
-               [-s SCHEMA] [-$ SCHEMA_URI]
?                                          -
+               [-s SCHEMA] [-$ SCHEMA_URI]
-               ...
?                  -
+               ...
- genson: error: noting to do - no schemas or objects given
?                                                          -
+ genson: error: noting to do - no schemas or objects given

Proposed solution

Add a method TestError.assertEqualIgnoreWhitespace() that replaces any chunk of whitespace with a single space before comparing two strings, and call this method when comparing stderr to the expected output.

Also considered

I looked through the Python unittest documentation, and I was unable to find an existing method that would ignore differences in whitespace when asserting equality between two strings.

I considered using a regular expression to standardize whitespace differences (one of the proposed solutions on this StackOverflow question), but that would add an import and (in my opinion) some unnecessary complexity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant