Edward,

On Fri, Dec 4, 2020 at 7:40 AM Edward K. Ream <[email protected]> wrote:

> I spent considerable time last week on more complicated schemes for the
> new unit tests. That complexity eventually collapsed. Only create_app
> remains. That collapse would likely not have happened were it not for Brian's
> comment
> <https://groups.google.com/g/leo-editor/c/DTUe32_WhUs/m/duDmZOPEBQAJ>:
>
> "I tend to favor composition over inheritance and functions over classes.
> The fixture feature helps with this preference."
>
> Thanks, Brian.
>

I'm glad my comment was helpful.


> Now I see that @test x is precisely equivalent to def test_x. Oh my, I
> have wasted so much time on feeble substitutes for features of python's
> unittest module.\
>

Oh well.  Don't be too hard on yourself :-).


I think the code you have for checking actual vs. expected results is
another opportunity for simplification.

When I change one of the test_editCommands.py expected results in order to
force it to fail and then run it with pytest, I see output like this:

        if s1 != s2:  # pragma: no cover
            print('mismatch in body')
            g.printObj(g.splitLines(s2), tag='expected')
            g.printObj(g.splitLines(s1), tag='got')
            print('parent_p.b', repr(self.parent_p.b))
>           assert False
E           AssertionError: assert False

leo/unittests/commands/test_editCommands.py:54: AssertionError


This is pytest being helpful and showing exactly where in the code the
failure came from.  Following that output, this is also displayed:

-----------------------------------------------------------------------------
Captured stdout call
------------------------------------------------------------------------------
mismatch in body
expected:
[
    'first line\n',
    '    line 1\n',
    '        line a\n',
    '            line b\n',
    '    line cXXXXXXx\n',
    'last line\n'
]
got:
[
    'first line\n',
    '    line 1\n',
    '        line a\n',
    '            line b\n',
    '    line c\n',
    'last line\n'
]
parent_p.b ''


This is your code being helpful by showing more details about how the
actual was different from the expected.

However pytest automatically tries to be similarly helpful if you are more
specific with your assertions.  IOW if you change the code from this:

        if s1 != s2:  # pragma: no cover
            print('mismatch in body')
            g.printObj(g.splitLines(s2), tag='expected')
            g.printObj(g.splitLines(s1), tag='got')
            print('parent_p.b', repr(self.parent_p.b))
           assert False

to this:

       assert s1 == s2

then the pytest output looks like this:

        s1 = self.tempNode.b
        s2 = self.after_p.b
>       assert s1 == s2
E       AssertionError: assert 'first line\n...\nlast line\n' == 'first
line\n...\nlast line\n'
E         Skipping 56 identical leading characters in diff, use -v to show
E         -     line c
E         +     line cXXXXXXx
E         ?           +++++++
E           last line


IMO, this is as good if not better than your output. The 'mismatch in body'
message is missing from this, but maybe renaming s1 and s2 to body1 and
body2 would be self-documenting and serve as a suitable replacement.

I get the feeling you are trying to avoid being dependent on pytest
features. But this approach will still be able to run using only unittest,
just that the output won't be as nice.

Brian

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/CAO5X8Cys9bUhJvMoArw9k2ohjYJ%3D%3DMkVKB1OhCtq8TnEDUNgtw%40mail.gmail.com.

Reply via email to