如何从脚本中重复运行python单元测试并收集结果

时间:2013-03-24 21:42:45

标签: python unit-testing python-unittest

我无法弄清楚如何在python脚本中运行单个单元测试并收集结果。

场景:我有一系列测试可以检查产生不同对象的各种统计分布的各种方法。测试有时会失败,因为他们应该给我基本上检查特定种类的随机性。我想从脚本甚至解释器中反复运行测试并收集结果以供进一步分析。

假设我有一个模块myTest.py:

class myTest(unittest.TestCase):
    def setup(self):
       ...building objects, etc....
    def testTest1(self):
           ..........
    def testTest2(self):
           ..........

基本上我需要:

  1. 运行设置方法
  2. 运行testTest1(比如说),100次
  3. 收集失败
  4. 返回失败
  5. 我最接近的是(使用类似问题的代码):

    from unittest import TextTestRunner, TestSuite
    runner = TextTestRunner(verbosity = 2)
    tests = ['testTest1']
    suite = unittest.TestSuite(map(myTest, tests))
    runner.run(suite)
    

    但这不起作用,因为:

    runner.run(suite)未运行设置方法

    我无法捕获它在testTest1失败时抛出的异常

1 个答案:

答案 0 :(得分:2)

您只需要将要运行多次的测试添加到套件中。

这是一个完整的代码示例。您还可以see this code running in an interactive Python console to prove that it does actually work

import unittest
import random

class NullWriter(object):
    def write(*_, **__):
        pass

    def flush(*_, **__):
        pass

SETUP_COUNTER = 0

class MyTestCase(unittest.TestCase):

    def setUp(self):
        global SETUP_COUNTER
        SETUP_COUNTER += 1

    def test_one(self):
        self.assertTrue(random.random() > 0.3)

    def test_two(self):
        # We just want to make sure this isn't run
        self.assertTrue(False, "This should not have been run")


def suite():
    tests = []
    for _ in range(100):
        tests.append('test_one')

    return unittest.TestSuite(map(MyTestCase, tests))

results = unittest.TextTestRunner(stream=NullWriter()).run(suite())
print dir(results)
print 'setUp was run', SETUP_COUNTER, 'times'