Search code examples
pythondjangopython-unittestflake8

Remove empty spaces in assertDictEqual to pass Flake8


I'm fixing code to normalize in PEP8 by Flake8. One of the problems is long lines that I need skip lines to accomplish Flake8.

This is my return that I skip line using :

        expected_return = {
            "error": {
                "message": None,
                "type": "You are logged as admin. This endpoint requires\
                         a customer or an anonymous user.",
                "detail": None,
            }
        }

Using from django.test import TestCase with Django, my assertion is:

self.assertDictEqual(self.deserialize(response.content), expected_return)

My test don't pass because the assert reads with spaces:

AssertionError: {u'error': {u'message': None, u'type': u'You are logged as admin. This endpoint  [truncated]... != {u'error': {u'message': None, u'type': u'You are logged as admin. This endpoint  [truncated]...
  {u'error': {u'detail': None,
              u'message': None,
-             u'type': u'You are logged as admin. This endpoint requires a customer or an anonymous user.'}}
+             u'type': u'You are logged as admin. This endpoint requires                         a customer or an anonymous user.'}}

I tried to use self.assertMultiLineEqual too, but other errors appers.

What solutions usually you use to skip lines, in accordance to PEP8 and do not receiving with this error with unittest?

Best regards.


Solution

  • Python supports concatenating strings by having the appear after each other:

        expected_return = {
            "error": {
                "message": None,
                "type": "You are logged as admin. This endpoint requires "\ 
                        "a customer or an anonymous user.",
                "detail": None,
            }
        }
    

    The end result is a concatenated string without any additional whitespace:

    "You are logged as admin. This endpoint requires a customer or an anonymous user."