mardi 22 décembre 2015

Python parameterized unittest by subclassing TestCase

How can I create multiple TestCases and run them programmatically? I'm trying to test multiple implementations of a collection on a common TestCase.

I'd prefer to stick to with plain unittest and avoid dependencies.

Here's some resources that I looked at that didn't quite meet what I wanted:

Here's a minimal (non)working example.

import unittest

MyCollection = set
AnotherCollection = set
# ... many more collections


def maximise(collection, array):
    return 2


class TestSubClass(unittest.TestCase):

    def __init__(self, collection_class):
        unittest.TestCase.__init__(self)
        self.collection_class = collection_class
        self.maximise_fn = lambda array: maximise(collection_class, array)


    def test_single(self):
        self.assertEqual(self.maximise_fn([1]), 1)


    def test_overflow(self):
        self.assertEqual(self.maximise_fn([3]), 1)

    # ... many more tests


def run_suite():
    suite = unittest.defaultTestLoader
    for collection in [MyCollection, AnotherCollection]:
        suite.loadTestsFromTestCase(TestSubClass(collection))
    unittest.TextTestRunner().run(suite)


def main():
    run_suite()


if __name__ == '__main__':
    main()

The above approach errors with in loadTestsFromTestCase:

TypeError: issubclass() arg 1 must be a class

Aucun commentaire:

Enregistrer un commentaire