python3: Fix dynamically added test methods

Usage of types.MethodType has changed and means a bound method in python3.
This is probably the reason why self must be passed explicitly in python3.

Signed-off-by: IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
IWAMOTO Toshihiro 2015-06-24 18:47:05 +09:00 committed by FUJITA Tomonori
parent 536a42d8c1
commit a23734444c
5 changed files with 30 additions and 9 deletions

View File

@ -17,6 +17,7 @@
import gettext
import os
import unittest
import six
import sys
import types
import logging
@ -262,4 +263,8 @@ def add_method(cls, method_name, method):
"""Add the method to the class dynamically, keeping unittest/nose happy."""
method.func_name = method_name
method.__name__ = method_name
setattr(cls, method_name, types.MethodType(method, None, cls))
if six.PY3:
methodtype = types.MethodType(method, cls)
else:
methodtype = types.MethodType(method, None, cls)
setattr(cls, method_name, methodtype)

View File

@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import six
import sys
import unittest
from nose.tools import eq_
@ -188,7 +189,7 @@ class Test_Parser(unittest.TestCase):
wire_msg)
json_dict2 = self._msg_to_jsondict(msg)
# XXXdebug code
open(('/tmp/%s.json' % name), 'wb').write(json.dumps(json_dict2))
open(('/tmp/%s.json' % name), 'w').write(json.dumps(json_dict2))
eq_(json_dict, json_dict2)
# json -> OFPxxx -> json
@ -247,12 +248,15 @@ def _add_tests():
if not fnmatch.fnmatch(file, '*.packet'):
continue
wire_msg = open(pdir + '/' + file, 'rb').read()
json_str = open(jdir + '/' + file + '.json', 'rb').read()
json_str = open(jdir + '/' + file + '.json', 'r').read()
method_name = ('test_' + file).replace('-', '_').replace('.', '_')
def _run(self, name, wire_msg, json_str):
print('processing %s ...' % name)
self._test_msg(name, wire_msg, json_str)
if six.PY3:
self._test_msg(self, name, wire_msg, json_str)
else:
self._test_msg(name, wire_msg, json_str)
print('adding %s ...' % method_name)
f = functools.partial(_run, name=method_name, wire_msg=wire_msg,
json_str=json_str)

View File

@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import six
import sys
import unittest
from nose.tools import eq_
@ -145,7 +146,10 @@ def _add_tests():
def _run(self, name, ofpp):
print('processing %s ...' % name)
self._test(name, ofpp)
if six.PY3:
self._test(self, name, ofpp)
else:
self._test(name, ofpp)
print('adding %s ...' % method_name)
f = functools.partial(_run, name=method_name,
ofpp=ofpp)

View File

@ -21,6 +21,7 @@ except ImportError:
# Python 2
pass
import six
import sys
import unittest
from nose.tools import eq_
@ -55,7 +56,7 @@ class Test_Parser_OFPMatch(unittest.TestCase):
match = ofpp.OFPMatch(**d)
b = bytearray()
match.serialize(b, 0)
match2 = match.parser(buffer(b), 0)
match2 = match.parser(six.binary_type(b), 0)
for k, v in d.items():
ok_(k in match)
ok_(k in match2)
@ -235,7 +236,10 @@ def _add_tests():
def _run(self, name, ofpp, d, domask):
print('processing %s ...' % name)
self._test(name, ofpp, d, domask)
if six.PY3:
self._test(self, name, ofpp, d, domask)
else:
self._test(name, ofpp, d, domask)
print('adding %s ...' % method_name)
f = functools.partial(_run, name=method_name,
ofpp=ofpp, d=d, domask=domask)

View File

@ -20,6 +20,7 @@ except ImportError:
# Python 2
pass
import six
import sys
import unittest
from nose.tools import eq_
@ -47,7 +48,7 @@ class Test_Parser_OFPStats(unittest.TestCase):
stats = ofpp.OFPStats(**d)
b = bytearray()
stats.serialize(b, 0)
stats2 = stats.parser(buffer(b), 0)
stats2 = stats.parser(six.binary_type(b), 0)
for k, v in d.iteritems():
ok_(k in stats)
ok_(k in stats2)
@ -193,7 +194,10 @@ def _add_tests():
def _run(self, name, ofpp, d):
print('processing %s ...' % name)
self._test(name, ofpp, d)
if six.PY3:
self._test(self, name, ofpp, d)
else:
self._test(name, ofpp, d)
print('adding %s ...' % method_name)
f = functools.partial(_run, name=method_name,
ofpp=ofpp, d=d)