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

boB Stepp robertvstepp at gmail.com
Mon Apr 17 00:01:35 EDT 2017


OK, between Alan and Martin I think that I see how to make the code
snippet actually test a *function* as the snippet seems to suggest.
Recollect that my original question(s) started:

On Sat, Apr 15, 2017 at 6:17 PM, boB Stepp <robertvstepp at gmail.com> wrote:
> In the section
>
> https://docs.python.org/3/library/test.html#writing-unit-tests-for-the-test-package
>
> I have been trying to make sense of the given pointer and code snippet:
>
> <quote>
> Try to maximize code reuse. On occasion, tests will vary by something
> as small as what type of input is used. Minimize code duplication by
> subclassing a basic test class with a class that specifies the input:
>
> class TestFuncAcceptsSequencesMixin:
>
>     func = mySuperWhammyFunction
>
>     def test_func(self):
>         self.func(self.arg)
>
> class AcceptLists(TestFuncAcceptsSequencesMixin, unittest.TestCase):
>     arg = [1, 2, 3]
>
> class AcceptStrings(TestFuncAcceptsSequencesMixin, unittest.TestCase):
>     arg = 'abc'
>
> class AcceptTuples(TestFuncAcceptsSequencesMixin, unittest.TestCase):
>     arg = (1, 2, 3)
>
> When using this pattern, remember that all classes that inherit from
> unittest.TestCase are run as tests. The Mixin class in the example
> above does not have any data and so can’t be run by itself, thus it
> does not inherit from unittest.TestCase.
> </quote>

The snippet as supplied will not run.  "mySuperWhammyFunction" is not
defined anywhere.  Additionally, the code to start the unittest
machinery going was not included (But to be fair, it was just
discussed above this snippet.).  In my original effort I tried to stay
as true as possible to the code snippet in the docs and only added the
"missing" elements I just mentioned.  However, I think the docs are
misleading with this line:

func = mySuperWhammyFunciton

and this line:

self.func(self.arg)

I asked myself, how am I now testing functions with unittest?  I've
been doing it for a few months now.  What I would do in the context of
this Mixin approach would be:

def mySuperWhammyFunction(any_input):
    return any_input

import unittest

class TestFuncAcceptsSequencesMixin:

    def test_func(self):
        f = mySuperWhammyFunction(self.arg)    # What need is there
for the class variable func?
        self.assertEqual(f, self.arg)          # Just call and assign
the function being tested directly!
        print(f)

class AcceptLists(TestFuncAcceptsSequencesMixin, unittest.TestCase):
    arg = [1, 2, 3]

class AcceptStrings(TestFuncAcceptsSequencesMixin, unittest.TestCase):
    arg = 'abc'

class AcceptTuples(TestFuncAcceptsSequencesMixin, unittest.TestCase):
    arg = (1, 2, 3)

if __name__ == '__main__':
    unittest.main()

This works fine and produces this output:

> python -m unittest -v test_super.py
test_func (test_super.AcceptLists) ... [1, 2, 3]
ok
test_func (test_super.AcceptStrings) ... abc
ok
test_func (test_super.AcceptTuples) ... (1, 2, 3)
ok

----------------------------------------------------------------------
Ran 3 tests in 0.002s

OK

Am I missing anything?  If not, then why did the code snippet use the
(I believe to be misleading.) class variable approach with "func =
mySuperWhammyFunction" and "self.func(self.arg)"?

boB


More information about the Tutor mailing list