summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYAMAMOTO Takashi <yamamoto@valinux.co.jp>2015-04-27 14:35:52 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2015-04-28 10:57:29 +0900
commitac7988f15b4dbee40a16f96726008b506f279932 (patch)
treed43637c3b5883ae7707d7e519df5666fdf9ce468
parent720dc37d237a48476dea1509b51033fa7908031d (diff)
tests: Fix dynamically added test cases
I haven't investigated since when it has been broken. The versions of unittest and nose might be relevant. Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/tests/test_lib.py24
-rw-r--r--ryu/tests/unit/ofproto/test_parser.py5
-rw-r--r--ryu/tests/unit/ofproto/test_parser_compat.py6
-rw-r--r--ryu/tests/unit/ofproto/test_parser_ofpmatch.py7
4 files changed, 31 insertions, 11 deletions
diff --git a/ryu/tests/test_lib.py b/ryu/tests/test_lib.py
index 49922b4f..562e9f9b 100644
--- a/ryu/tests/test_lib.py
+++ b/ryu/tests/test_lib.py
@@ -1,7 +1,24 @@
+# Copyright (C) 2013,2014,2015 Nippon Telegraph and Telephone Corporation.
+# Copyright (C) 2013,2014,2015 YAMAMOTO Takashi <yamamoto at valinux co jp>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
import gettext
import os
import unittest
import sys
+import types
import logging
from nose import result
@@ -240,3 +257,10 @@ def run_tests(c=None):
verbosity=c.verbosity,
config=c)
return not core.run(config=c, testRunner=runner)
+
+
+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))
diff --git a/ryu/tests/unit/ofproto/test_parser.py b/ryu/tests/unit/ofproto/test_parser.py
index c197b9e7..a567039e 100644
--- a/ryu/tests/unit/ofproto/test_parser.py
+++ b/ryu/tests/unit/ofproto/test_parser.py
@@ -27,6 +27,7 @@ from ryu.ofproto import ofproto_v1_2
from ryu.ofproto import ofproto_v1_3
from ryu.ofproto import ofproto_v1_4
from ryu.ofproto import ofproto_v1_5
+from ryu.tests import test_lib
import json
@@ -257,9 +258,7 @@ def _add_tests():
print('adding %s ...' % method_name)
f = functools.partial(_run, name=method_name, wire_msg=wire_msg,
json_str=json_str)
- f.func_name = method_name
- f.__name__ = method_name
- setattr(Test_Parser, method_name, f)
+ test_lib.add_method(Test_Parser, method_name, f)
cases.add(method_name)
n_added += 1
assert n_added > 0
diff --git a/ryu/tests/unit/ofproto/test_parser_compat.py b/ryu/tests/unit/ofproto/test_parser_compat.py
index 7ebdf98f..cacae58b 100644
--- a/ryu/tests/unit/ofproto/test_parser_compat.py
+++ b/ryu/tests/unit/ofproto/test_parser_compat.py
@@ -25,6 +25,7 @@ from ryu.ofproto import ofproto_v1_2_parser
from ryu.ofproto import ofproto_v1_3_parser
from ryu.lib import addrconv
+from ryu.tests import test_lib
from struct import unpack
@@ -148,9 +149,6 @@ def _add_tests():
print('adding %s ...' % method_name)
f = functools.partial(_run, name=method_name,
ofpp=ofpp)
- f.func_name = method_name
- f.__name__ = method_name
- cls = Test_Parser_Compat
- setattr(cls, method_name, f)
+ test_lib.add_method(Test_Parser_Compat, method_name, f)
_add_tests()
diff --git a/ryu/tests/unit/ofproto/test_parser_ofpmatch.py b/ryu/tests/unit/ofproto/test_parser_ofpmatch.py
index 2436b932..86da2895 100644
--- a/ryu/tests/unit/ofproto/test_parser_ofpmatch.py
+++ b/ryu/tests/unit/ofproto/test_parser_ofpmatch.py
@@ -30,6 +30,7 @@ from ryu.ofproto import ofproto_v1_2
from ryu.ofproto import ofproto_v1_3
from ryu.ofproto import ofproto_v1_2_parser
from ryu.ofproto import ofproto_v1_3_parser
+from ryu.tests import test_lib
class Test_Parser_OFPMatch(unittest.TestCase):
@@ -238,9 +239,7 @@ def _add_tests():
print('adding %s ...' % method_name)
f = functools.partial(_run, name=method_name,
ofpp=ofpp, d=d, domask=domask)
- f.func_name = method_name
- f.__name__ = method_name
- cls = Test_Parser_OFPMatch
- setattr(cls, method_name, f)
+ test_lib.add_method(Test_Parser_OFPMatch,
+ method_name, f)
_add_tests()