Skip to content

Instantly share code, notes, and snippets.

@audiolion
Last active March 24, 2023 01:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audiolion/fcba115f74b99d980bd6124f4b051fd6 to your computer and use it in GitHub Desktop.
Save audiolion/fcba115f74b99d980bd6124f4b051fd6 to your computer and use it in GitHub Desktop.
pytest assert vs self.assertEqual
class TestSomething(TestCase):
def test_something(self):
assert something['message'] == 'Asserting that a very long message statement is as expected in this test case here, the issue is that its all on one line with no easy way to conform to PEP-8 besides adding "\" to continue the one line statement in python.'
self.assertEqual(
something['message'],
('Asserting that a very long message statement is as expected'
' in this test case here, the issue is that its all on one line'
' with no easy way to conform to PEP-8 besides adding "\" to'
' continue the one line statement in python.'
' With assertEqual and the function structure we can easily'
' break up a long assert statement to make it readable and'
' PEP-8 compliant.')
)
@okken
Copy link

okken commented Oct 12, 2017

In this case, I'd recommend using parens to define a variable expected and assert equality with that.
I've put this into a runnable file, with one difference in the text.

test_example.py

from unittest import TestCase

something = {}
something['message'] = ('Asserting that a very long message statement is as expected'
                        ' in this test case here, the issue is that its all on one line'
                        ' with no easy way to conform to PEP-8 besides adding "\" to'
                        ' continue the one line statement in python.'
                        ' With assertEqual and the function structure we can easily'
                        ' break up a long assert statement to make it readable and'
                        ' PEP-8 compliant.')

class TestSomething(TestCase):
    def test_something(self):
        self.assertEqual(
          something['message'],
          ('Asserting that a very long message statement is as expected'
           ' in this test case here, the issue is that its all on one line'
           ' with no easy way to conform to PEP-8 besides adding "\" to'
           ' continue the one line statement in python.'
           ' With assertEqual And the function structure we can easily'
           ' break up a long assert statement to make it readable and'
           ' PEP-8 compliant.')
        )

    def test_alt(self):
        expected = ('Asserting that a very long message statement is as expected'
                    ' in this test case here, the issue is that its all on one line'
                    ' with no easy way to conform to PEP-8 besides adding "\" to'
                    ' continue the one line statement in python.'
                    ' With assertEqual And the function structure we can easily'
                    ' break up a long assert statement to make it readable and'
                    ' PEP-8 compliant.')
        assert something['message'] == expected

Run it and you'll see that the pytest assert description is way better. :)
The pytest version points directly to the character that's different.

assertEqual:

F
test_thing.py:12 (TestSomething.test_something)
self = <test_thing.TestSomething testMethod=test_something>

    def test_something(self):
        self.assertEqual(
          something['message'],
>         ('Asserting that a very long message statement is as expected'
           ' in this test case here, the issue is that its all on one line'
           ' with no easy way to conform to PEP-8 besides adding "\" to'
           ' continue the one line statement in python.'
           ' With assertEqual And the function structure we can easily'
           ' break up a long assert statement to make it readable and'
           ' PEP-8 compliant.')
        )
E       AssertionError: 'Asse[231 chars]qual and the function structure we can easily [69 chars]ant.' != 'Asse[231 chars]qual And the function structure we can easily [69 chars]ant.'
E       Diff is 1215 characters long. Set self.maxDiff to None to see it.

test_thing.py:16: AssertionError

assert (pytest assert rewritten version):

F
test_thing.py:24 (TestSomething.test_alt)
self = <test_thing.TestSomething testMethod=test_alt>

    def test_alt(self):
        expected = ('Asserting that a very long message statement is as expected'
                    ' in this test case here, the issue is that its all on one line'
                    ' with no easy way to conform to PEP-8 besides adding "\" to'
                    ' continue the one line statement in python.'
                    ' With assertEqual And the function structure we can easily'
                    ' break up a long assert statement to make it readable and'
                    ' PEP-8 compliant.')
>       assert something['message'] == expected
E       AssertionError: assert 'Asserting th...-8 compliant.' == 'Asserting tha...-8 compliant.'
E         Skipping 230 identical leading characters in diff, use -v to show
E         Skipping 104 identical trailing characters in diff, use -v to show
E         - sertEqual and the fu
E         ?           ^
E         + sertEqual And the fu
E         ?           ^

test_thing.py:33: AssertionError

@acurvers
Copy link

would you recommend to always use pytest assert ?

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