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

boB Stepp robertvstepp at gmail.com
Sat Apr 15 19:17:40 EDT 2017


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>

I have tried to implement this in various ways, but cannot overcome a
basic hurdle where I get an argument mismatch.  Following is my
simplest effort to implement the above which does not actually test
anything yet, but demonstrates this mismatch hurdle:

--------------------------------------------------------------------------------------
#!/usr/bin/env python3

def mySuperWhammyFunction(any_input):
    return any_input

import unittest

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)

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

This gives me the following result:

--------------------------------------------------------------------------------------
> python -m unittest test_super.py
EEE
======================================================================
ERROR: test_func (test_super.AcceptLists)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\Projects\test_super.py", line 13, in test_func
    self.func(self.arg)
TypeError: mySuperWhammyFunction() takes 1 positional argument but 2 were given

======================================================================
ERROR: test_func (test_super.AcceptStrings)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\Projects\test_super.py", line 13, in test_func
    self.func(self.arg)
TypeError: mySuperWhammyFunction() takes 1 positional argument but 2 were given

======================================================================
ERROR: test_func (test_super.AcceptTuples)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\Projects\test_super.py", line 13, in test_func
    self.func(self.arg)
TypeError: mySuperWhammyFunction() takes 1 positional argument but 2 were given

----------------------------------------------------------------------
Ran 3 tests in 0.000s

FAILED (errors=3)
--------------------------------------------------------------------------------------

I suspect that both an object instance and self.arg is getting passed
to mySuperWhammyFunction(), but I am not seeing how the object
instance is getting passed -- if my suspicion is indeed correct.

I also am not truly understanding how this code is working within the
unittest framework.

Help!

TIA!

boB


More information about the Tutor mailing list