我有几个测试模块,都是通过一个驱动脚本一起调用的,该脚本可以接受各种参数。这些测试本身是使用Python的unittest模块编写的。
import optparse
import unittest
import sys
import os
from tests import testvalidator
from tests import testmodifier
from tests import testimporter
#modify the path so that the test modules under /tests have access to the project root
sys.path.insert(0, os.path.dirname(__file__))
def run(verbosity):
if verbosity == "0":
sys.stdout = open(os.devnull, 'w')
test_suite = unittest.TestSuite()
test_suite.addTest(unittest.TestLoader().loadTestsFromTestCase(testvalidator.TestValidator))
test_suite.addTest(unittest.TestLoader().loadTestsFromTestCase(testmodifier.TestModifier))
test_suite.addTest(unittest.TestLoader().loadTestsFromTestCase(testimporter.TestDataImporter))
unittest.TextTestRunner(verbosity=int(verbosity)).run(test_suite)
if __name__ == "__main__":
#a simple way to control output verbosity
parser = optparse.OptionParser()
parser.add_option("--verbosity", "--verbosity", dest="verbosity", default="0")
(options, args) = parser.parse_args()
run(options.verbosity)
我的问题是,在这些测试模块中,我有一些测试想要根据传递给驱动程序的不同参数跳过。我知道unittest提供了一系列的装饰器来实现这个功能,但我不知道将这些信息传递给各个模块的最佳方法。例如,如果我有一个--skip-slow
参数,那么我该如何注释慢速测试,并跳过它们呢?
感谢您的时间。