试图继承自unittest.TestCase和另一个类

时间:2016-07-01 14:47:45

标签: python unit-testing multiple-inheritance python-unittest

我有一个基类(仅从object继承 ),并对一组排序算法进行常见测试。现在,对于每个特定的算法,我想创建一个测试类,该类继承自unittest.TestCase和此类,并对所有排序算法进行常见测试。

例如,我想创建一个类来测试,比如说冒泡排序。目前,我正在做:

import unittest

from ands.algorithms.sorting import bubble_sort
from tests.algorithms.sorting.base_tests import *


class TestBubbleSort(unittest.TestCase, SortingAlgoTests):

    def __init__(self):
        unittest.TestCase.__init__(self)
        SortingAlgoTests.__init__(self, bubble_sort, True)


if __name__ == "__main__":
    unittest.main(verbosity=2)

现在,当我使用以下命令运行TestBubbleSort时:

coverage run -m unittest discover . -v

我收到一堆错误,如:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/__main__.py", line 18, in <module>
    main(module=None)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/main.py", line 93, in __init__
    self.parseArgs(argv)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/main.py", line 117, in parseArgs
    self._do_discovery(argv[2:])
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/main.py", line 228, in _do_discovery
    self.test = loader.discover(self.start, self.pattern, self.top)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/loader.py", line 341, in discover
    tests = list(self._find_tests(start_dir, pattern))
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/loader.py", line 398, in _find_tests
    full_path, pattern, namespace)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/loader.py", line 452, in _find_test_path
    return self.loadTestsFromModule(module, pattern=pattern), False
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/loader.py", line 123, in loadTestsFromModule
    tests.append(self.loadTestsFromTestCase(obj))
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/loader.py", line 92, in loadTestsFromTestCase
    loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/suite.py", line 24, in __init__
    self.addTests(tests)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/suite.py", line 57, in addTests
    for test in tests:
TypeError: __init__() takes 1 positional argument but 2 were given

由于我对这个模块unittest很新,但我已经为Java项目创建了单元测试,我不确定是什么问题。

我注意到如果我没有__init__方法,即TestBubbleSort看起来像这样:

import unittest

from ands.algorithms.sorting import bubble_sort
from tests.algorithms.sorting.base_tests import *


class TestBubbleSort(unittest.TestCase, SortingAlgoTests):
    pass

if __name__ == "__main__":
    unittest.main(verbosity=2)

我不再有上述错误。问题是我需要将排序算法传递给SortingAlgoTests基类,所以我需要调用它的构造函数,通常我会在__init__方法中执行它。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

我设法通过将TestBubbleSort修改为:

来解决问题
class TestBubbleSort(unittest.TestCase, SortingAlgoTests):

    def __init__(self, methodName="runTest"):
        unittest.TestCase.__init__(self, methodName)
        SortingAlgoTests.__init__(self, bubble_sort, True)

显然,要求unittest.TestCase的子类具有__init__方法,其签名与上面的签名类似,即带有参数methodName,需要传递到unittest.TestCase的超级构造函数。

答案 1 :(得分:0)

看起来你在SortingAlgoTests类中重写了addTest方法