[apparmor] [patch] add better loop support to common_test.py

Steve Beattie steve at nxnw.org
Tue Mar 31 19:34:37 UTC 2015


On Tue, Mar 31, 2015 at 03:28:33PM +0200, Christian Boltz wrote:
> Hello,
> 
> Am Dienstag, 31. März 2015 schrieb Steve Beattie:
> > Sorry for the delay in reviewing.
> 
> Being able to taunt about the pending reviews also has its advantages ;-))  
> (and I'm quite sure you won't write something like "to keep the number 
> of pending patches high..." again *eg*)
> 
> > On Fri, Mar 06, 2015 at 11:57:11PM +0100, Christian Boltz wrote:
> ...
> > > Here's v3, this time with the number included in the filename ;-)
> > 
> > Unfortunately, this doesn't appear to work under python2:
> > 
> >   $ PYTHONPATH=.. python2 test-example.py
> >   Traceback (most recent call last):
> >     File "test-example.py", line 44, in <module>
> >       setup_all_tests()
> >     File "/home/steve/bzr/apparmor-master/utils/test/common_test.py",
> > line 60, in setup_all_tests obj_instance = obj() # will fail for
> > (non-test) classes with additional __init__ parameters File
> > "/usr/lib/python2.7/unittest/case.py", line 189, in __init__
> > (self.__class__, methodName))
> >   ValueError: no such test method in <class 'common_test.AATest'>:
> > runTest
> > 
> > I suspect this has to do with the differences in how objects are
> > internally implemented between python 2 and python 3.
> 
> Actually not - it's a behaviour difference (or a missing bugfix?) in 
> py2 unittest vs. py3 unittest. Just let me paste some code:
> 
> 
> === /usr/lib64/python3.4/unittest/case.py ===
> 
> class TestCase(object):
>     # ...
>     def __init__(self, methodName='runTest'):
>         # ...
>         try:
>             testMethod = getattr(self, methodName)
>         except AttributeError:
>             if methodName != 'runTest':
>                 # we allow instantiation with no explicit method name
>                 # but not an *incorrect* or missing method name
>                 raise ValueError("no such test method in %s: %s" %
>                       (self.__class__, methodName))
> 
> 
> === /usr/lib64/python2.7/unittest/case.py ===
> 
> class TestCase(object):
>     # ...
>     def __init__(self, methodName='runTest'):
>         # ...
>         try:
>             testMethod = getattr(self, methodName)
>         except AttributeError:
>             raise ValueError("no such test method in %s: %s" %
>                   (self.__class__, methodName))
> 
> 
> The difference should be obvious ;-)
> 
> 
> A simple solution would be to add a dummy
> 
>     def runTest(self):
>         pass
> 
> to AATest. Besides incrementing the number of tests run, it doesn't seem 
> to hurt - but it still feels dirty :-/
> 
> 
> After some thinking (and reading documentation) - I noticed that 
> isinstance() makes things more difficult difficult than needed.
> 
> The better way is using issubclass(), which means I don't need to 
> create a temporary instance (which also means I don't need the 
> try/except), and also don't hit the failure you noticed.

Ah, yeah, I like this solution much better, thanks.

> [ 07-common_test_better_loop_support.diff ]

Acked-by: Steve Beattie <steve at nxnw.org>

-- 
Steve Beattie
<sbeattie at ubuntu.com>
http://NxNW.org/~steve/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <https://lists.ubuntu.com/archives/apparmor/attachments/20150331/59dd4472/attachment.pgp>


More information about the AppArmor mailing list