AddSetupclass modeled addCleanup clean up resources in the unittest

addCleanup use cases presented here does not, you can look at my other articles compiled:  Python unittest framework addCleanup function Detailed

But if this scenario below, still left a large number of physical resources

The pilot injection of various resources in setUpClass, such as:

1, into a

2, injection b

3, injection of c

Then clean up the teardownclass

1, cleaning c

2, cleaning b

3, a clean-up

Assume a mistake when filling b, then the first step would be an error in the teardownclass

 

Solution:

We modeled addCleanup write such a function, as follows

1, first define a list of setupclass

2, write SetupCleanup such a function and doSetupCleanup function, list in reverse order

3, in the doSetupCleanup added tearDownClass

code show as below:

    @classmethod
    def setUpClass(cls):
         ....
         cls.setup_cleanup_list = []
         ....
    @classmethod
    def SetupCleanup(cls, func, *args, **kwargs):
        cls.setup_cleanup_list.append((func, args, kwargs))

    @classmethod
    def doSetupcleanup(cls):
        try:
            for item in reversed(cls.setup_cleanup_list):
                function, args, kwargs = item
                for _ in range(3):
                    try:
                        function(*args, **kwargs)
                        break
                    except Exception as e:
                        log.exception(e)
        finally:
            cls.setup_cleanup_list = []
    @classmethod
    def tearDownClass(cls):
        cls.doSetupcleanup()

 

Guess you like

Origin www.cnblogs.com/landhu/p/11277719.html