Python unittest可以自动重新尝试失败的测试用例/套件吗?

时间:2011-08-22 19:19:11

标签: python unit-testing user-interface

简短问题
是否可以根据预定义的功能在故障/错误N次 OR 时重新尝试单元测试。 (就像用户的提示一样)

背景
为避免重新输入整页系统信息,请参阅passing data to unittestsauto test discovery上的SO问题,了解有关我的实际设置的详细信息。

关于手头的问题,我知道我可以通过重新编写我的测试用例来循环,直到它获得所需的结果(参见下面的伪代码),然后基于此断言。但是我宁愿不去重写100个测试用例。

我会有人指出,如果单位测试失败,它应该失败并完成。我同意这个100%如果人为错误可以删除。这是我连接的物理系统,很多时候数字万用表的引线连接不好,并且由于连接松动而可能会失败。

伪解决方法

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:
                # Run the test
                results = measure_voltage_from_system() 

                # Analyze Results
                test_status = pf.value_within_range(results, REGULATOR_5V_LOW, REGULATOR_5V_HIGH)

            catch:
                error_detected = True

            # Retest Failed Cards
            if(test_status == False):
                keep_running = rawinput('Test FAILED: Retry Test? (y/n):')
            elif(error_detected == True):
                keep_running = rawinput('Test ERROR: Retry Test? (y/n):')
            else:
                keep_running = 'n'

        # Inform the user on the test results 
        self.assertTrue(test_status,  'FAIL: 5V Regulator (' +str(results)+ ') Out of Range!')

编辑8/22/11 3:30 PM CST
我知道在这个用例中我违反了单元测试的定义。这些问题/评论也在我的一些其他SO问题中得到解决。我们选择的设计目标之一是利用现有框架来避免“重新发明轮子”。我们选择python的unittest的事实不是基于它的定义,而是执行和显示一系列测试的灵活性和稳健性。

进入这个项目,我知道会有一些事情需要解决方法,因为这个模块不适合这种用途。在这个时间点,我仍然相信这些变通办法比重写我自己的测试运行员更容易/更便宜。

编辑8/22/11 5:22 PM CST
我并没有以这种方式将unittest用于未来的项目,但是我还是坚持使用现有的框架工作来避免重复别人的努力。下面的评论是这个pycopia-QA似乎非常适合这个项目的一个例子。我当前项目的唯一缺点就是我已经编写了数百个单元测试用例,如果我要重写它们,这将是一项非常大的工作(注意它也将是一项非资助的工作)

编辑8/24/11 11:00 AM CST
未来的项目可能很清楚,为这种类型的测试切换到更加量身定制的框架工作。但是我仍然使用unittest运行项目,因此仍然需要使用unittest(或nose + 3rd addon)的解决方案。

5 个答案:

答案 0 :(得分:4)

用于编写Python单元测试的Python unittest模块。 ;-)它不太适合其他类型的测试。 nose包也是一个单元测试框架。

我在Python中编写了几个用于测试系统的测试框架。系统可以通过各种接口进行分发和自动化。两个是开源的。

Pycopia project是在Linux上运行的Python模块的集合。它是作为命名空间子包的集合提供的,其中一个是QA包,它是一个测试框架。

此子集的分支名为powerdroid,旨在控制通过物理测量(例如电压,电流等)的仪器。 RS-232,IEEE-488等。它为linux-gpib项目提供了另一种Python接口。

所以你可以从这些开始,而不是“重新发明轮子”,如果你愿意的话。您可能不必丢弃现有测试,因为框架可以调用任何子进程,您可以使用它启动现有测试。这也可以在Linux上运行。

答案 1 :(得分:4)

原问题后4年 - 我希望有人会关心:) 这是我在单元测试中做到这一点的解决方案。它有点丑陋,依赖于TestCase基类的实现,但它确实有效。

class MyTest(unittest.TestCase):
    ###
    ### Insert test methods here
    ###

    # Wrapping each test method so that a retry would take place.  
    def run(self, result=None):
        self.origTestMethodName = self._testMethodName
        self._testMethodName = "_testRetryWrapper"
        super(MyTest, self).run(result)
        self._testMethodName = self.origTestMethodName

    def _testRetryWrapper(self):
        testMethod = getattr(self, self.origTestMethodName)
        retryAttemptsLeft = settings.testRetryCount

        while True:
            try:
                testMethod()
                break
            except:
                if retryAttemptsLeft == 0:
                    raise
                else:
                    retryAttemptsLeft = retryAttemptsLeft - 1

答案 2 :(得分:1)

我认为您应该编写自己的专用框架 - 您可以在python的单元测试中对其进行建模,但这显然不是单元测试。您需要进行一些更改,例如,允许单独跳过,重试或重新验证每个测试 - 然后最终可能选择重新访问并重新测试失败的测试,最后显示统计数据测试第一次通过,他们花了多长时间,哪个测试时间最长,需要多少次重试,等等。

答案 3 :(得分:1)

我稍微改进了ShlomiKirály的答案,因此它不违反unittest框架并且跳过测试用例仍然有效:

class MyTest(unittest.TestCase):

#Eanble retries if specified in configuration file by attribute testRetryCount
def run(self, result=None):
    self.origTestMethodName = self._testMethodName
    retryAttemptsLeft = configuration.testRetryCount

    failuresBefore = len(result.failures) #check how many tests that are marked as failed before starting
    errorsBefore = len(result.errors) #check how many tests that are marked as failed before starting

    super(MyTest, self).run(result)
    if failuresBefore < len(result.failures): # If last test failed
        while True:
            if retryAttemptsLeft == 0:
                self.logger.error("Test failed after "+str(configuration.testRetryCount+1)+" attempts")
                break
            else:
                result.failures.pop(-1) #Removing last failure result
                self.logger.error("Test failed - retryAttemptsLeft: "+str(retryAttemptsLeft))
                retryAttemptsLeft = retryAttemptsLeft - 1

                super(MyTest, self).run(result)

    elif errorsBefore < len(result.errors): # If last test failed due to error
        while True:
            if retryAttemptsLeft == 0:
                self.logger.error("Test error after "+str(configuration.testRetryCount+1)+" attempts")
                break
            else:
                result.errors.pop(-1) #Removing last error result
                self.logger.error("Test error - retryAttemptsLeft: "+str(retryAttemptsLeft))
                retryAttemptsLeft = retryAttemptsLeft - 1

                super(MyTest, self).run(result)

答案 4 :(得分:0)

这里有点脏,但是很简单。使用unittest与其他测试一起运行测试并检测潜在的崩溃。但是,不要使用内置的断言,而是使用ifraise Exception编写自己的代码。然后将retrytenacity一起使用。例如:

from tenacity import retry


class MyTestCase(unittest.TestCase):
    @retry
    def test_with_retry:
        if not a == b:
            raise Exception("The test has failed")