[Tutor] Do not understand code snippet from "26.8. test — Regression tests package for Python"

boB Stepp robertvstepp at gmail.com
Mon Apr 17 22:05:41 EDT 2017


On Sun, Apr 16, 2017 at 11:50 PM, Mats Wichmann <mats at wichmann.us> wrote:

>
> You got me thinking as well, as I don't much care for unittest, at least
> partly because it forces you to use classes even when it doesn't feel
> all that natural.

I have looked into pytest multiple times, but have decided to stick
with unittest until I feel I have mastered its use.  While it forces
the use of classes, this is an advantage for me as I am still in the
beginning stages of learning OOP.  But if I ever make it through these
learning journeys, I will probably switch to using pytest.  Everything
I have read on it to this point has favorably impressed me.

[snip]

> === reverser.py ==
> def slicerev(collection):
>     return collection[::-1]
>
> if __name__ == "__main__":
>     print slicerev([1,2,3,4])
>     print slicerev((1,2,3,4))
>     print slicerev('abcd')
> ===

[snip]

> The actual test function should be pretty straightforward.
>
> === test_slicerev.py ===
> import pytest
>
> from reverser import slicerev
>
> @pytest.fixture(params=[
>     ([1,2,3,4], [4,3,2,1]),
>     ((1,2,3,4), (4,3,2,1)),
>     ('abcd',    'edcba')
>     ])
> def slicedata(request):
>     return request.param
>
> def test_slicerev(slicedata):
>     input, expected = slicedata
>     output = slicerev(input)
>     assert output == expected
> ===

It's funny you picked this type of example.  Last year I was
struggling with getting unittest to feed in data to my test of a
function (or was it a method?), and almost took the plunge and went
all in on pytest because of the apparent ease of handling these types
of situations while respecting DRY.  I did find a way to do something
similar in unittest, so put off pytest for another day.  I cannot
remember now what I did.  I need to go back and find that code (If I
still have it.) and compare it with this Mixin approach that I started
this whole thread with.  Nonetheless pytest is definitely on my radar
and I will get to it at some point.

Thanks!


-- 
boB


More information about the Tutor mailing list