I have a base class (which inherits only from object
) with common tests for a set of sorting algorithms. Now, for each specific algorithm, I would like to create a test class which inherits both from unittest.TestCase
and this class with common tests for all sorting algorithms.
For example, I would like to create a class to test, say, a bubble sort. Currently, I'm doing:
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)
Now, when I run TestBubbleSort
using the command:
coverage run -m unittest discover . -v
I get a bunch of errors like:
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
Since I'm very new with this module unittest
, but I had already created unit tests for a Java project, I'm not sure what's the problem.
I noticed that if I don't have the __init__
method, i.e., TestBubbleSort
looks like this:
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)
I don't have the errors above anymore. The problem is that I need to pass the sorting algorithm to the SortingAlgoTests
base class, so I need to call its constructor, and usually I would do it in the __init__
method.
How can I solve this problem?
Aucun commentaire:
Enregistrer un commentaire