mardi 16 juin 2020

Single test method collecting results of other test methods (unittest package)

I am writing some tests for a algorithm, where the execution on the test set is quite expensive. To do so, I essentially wrote the following:

class MyAlgoTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.results = {expensive_computation(x) for x in something}
    def assertResultFound(self, single_result):
        if single_result in self.results:
            self.results.remove(single_result)

With that, I can use tests in the following way

class SpecificTestCase(MyAlgoTestCase)
    def test_result_found1(self):
        self.assertResultFound('foo')
        self.assertResultFound('bar')
    def test_result_found2(self):
        self.assertResultFound('baz')

Everything works like a charm. But here comes one problem:

In the end, I want to test that the set of results is empty (my algorithm found everything it was inteded to find).

I already tried the following:

  • Perform Test during tearDownClass. Drawback here: When I run python -m unittest ..., tearDownClass appers as test, which is ugly to my taste.
  • Write the respective test as method of MyAlgoTest. By that, I can not ensure, that this test runs as last. So some elements remain in the set of results that are removed later. Hence, the test falsely fails.

Please no answers like "these are no unittests - use something different" (I saw a lot answers of that kind here). This framework is the same over our whole DevOps environment.

Aucun commentaire:

Enregistrer un commentaire