0

I need to run a dynamic test in Python following an input coming from a Json, the input is something like

[["Element1", ["Test1", "Test2"]], ["Element2", ["Test3", "Test4 "]], ["Element3", ["Test1", "Test5", "Test6"]]]

So I was expecting three unittest.TestCase called Element1, Element2, Element3. Each of these TestCases have N def called TestX

Example:
Element 1
\-\> Test1
\-\> Test2
Element 2
\-\> Test3
\-\> Test4
Element 3
\-\> Test1
\-\> Test5
\-\> Test6

Currently I have tried with the Parametrized decorator (class and expand), Metaclass and subtest. I found the best result with a mix of metaclass and subtest, but I can't see the print made by subtest in the report. With the parameterized decorator the problem is that I can parameterize class or def, but they are not related, so I can't do Element 1 with exactly test1 and test2, then Element2 with Test3 and Test4, etc

import unittest


l = [["Element1", ["Test1", "Test2"]], ["Element2", ["Test3", "Test4 "]], ["Element3", ["Test1", "Test5", "Test6"]]]

def dynamic_test(nameTest):
    print("Write here the test based on name")
    return True

class RecipeTestMeta(type):
    def __new__(cls, name, bases, attrs):
        for element in l:
            test_list = element[1]
            attrs["test_{}".format(element[0])] = cls.gen(test_list)

        return super(RecipeTestMeta, cls).__new__(cls, name, bases, attrs)

    @classmethod
    def gen(cls, test_list):
        def fn(self):
            for test in test_list:
                with self.subTest(msg=test):
                    print("Hello")
                    self.assertTrue(dynamic_test(test), None)
        return fn

class MultiTest(unittest.TestCase, metaclass=RecipeTestMeta):
    def test_myTest(self):
        print("This is a print into a simple test")
        self.assertTrue(True)

In this case my result it's this:

Report by PyCharm Report HTML

In this case the real problem is that I can't see all the prints made by the test Different is the following simple test "test_myTest": Report by PyCharm

Does anyone have any suggestions? I thought the solution is to be able to put the prints in the subTests, but I haven't found a solution, I'm also listening to workarounds Thanks a lot

  • I feel like there is a language issue here. Some of your sentences use English words, but make no sense. I also don't understand what the actual problem is. Can you please just make your example reproducible? How does someone without Pycharm execute your tests (in a terminal)? What is the output you expect? And what is the actual output you are getting. These Pycharm screenshots are not helpful. – Daniil Fajnberg Jan 20 '23 at 14:44

0 Answers0