Alex Rivera | Logout

Can Python unittest automatically reattempt a failed testcase / suite?

Asked 2011-08-22T19:19:10.980
9

Short Question
Is it possible to re-attempt a unittest upon failure / error N number of times OR based on a predefined function. (Like a user's prompt)

Background
To avoid retyping an entire page of system information, please see SO question on passing data to unittests and on auto test discovery for more details on my physical set up.

Regarding the question at hand, I know I can do it by re-writting my test cases to loop until it gets the required results (see pseudo code below) then assert based off of this. However I rather not go and rewrite 100's of test cases.

I there will be someone that will point out that if a unittest fails, it should just fail and be done. I agree with this 100% if human error could be removed. This is a physical system that I am connected to and there are many times the leads from the digital multimeter are not connected well and it could fail because of a loose connection.

Pseudo Workaround

class Suite_VoltageRegulator(unittest.TestCase):
    def test_voltage_5v_regulator(self):   
        keep_running = 'y'
        error_detected = False

        print '\n'     
        # Display User Test Configuration
        msg = \
            '1) Connect Front DMM GND(black) to the TP_COM\n' +\
            '2) Connect Front DMM POS(red) to the TP-A\n' +\
            '3) Ensure the DMM terminal button indicates FRONT'

        continue_test = prompt.Prompt_Ok_Cancel('User Action Required!', msg)

        if not continue_test:
            self.assertTrue(False, 'User Canceled Test!')

        while(keep_running == 'y'):
            try:
               
Edit
Report

1 Answer

4

The Python unittest module in intended for writing Python unit tests. ;-) It's not so well suited for other kinds of testing. The nose package is also a unit test framework.

I have written several testing frameworks in Python that are designed to test systems. The systems can be distributed, and automated with various interfaces. Two are open-source.

The Pycopia project is a collection of Python modules that runs on Linux. It is provided as a collection of namespace subpackages, one of which is the QA package that is a testing framework.

A subset-fork of this is named powerdroid, and it is intended to control instrumentation for taking physical measurements (such as voltage, current, etc.) via. RS-232, IEEE-488,etc. It provides an alternative Python interface to the linux-gpib project.

So you may start with these, rather than "reinvent the wheel", if you want. You may not have to throw away existing tests, since the framework can invoke any subprocess you can start existing tests with it. This also runs on Linux.

answered 2011-08-23T01:12:21.957

Your Answer