2024-05-15 23:13:32 发布
网友
我在pwd中保存了一组单元测试脚本。我希望能够计算将要执行的单元测试(nosetests)的数量(不实际执行它们),并将该数量返回到python变量中,如下所示:
nosetests
>>> number_of_unit_tests = count_unit_tests('.') >>> number_of_unit_tests 400
我知道我可以从命令行收集:
nosetests --collect-only
但是,是否可以在脚本中执行此操作?你知道吗
您可以从python脚本运行any nose命令,如basic nose usage中所述,唯一的技巧是提取测试的数量。我看了一下functional tests in nose,觉得这样应该行得通,但您可以进一步缩小它:
import sys import unittest from cStringIO import StringIO import nose from nose.result import _TextTestResult class TestRunner(unittest.TextTestRunner): def _makeResult(self): self.result = _TextTestResult( self.stream, self.descriptions, self.verbosity) return self.result def count_unit_tests(module_name): stream = StringIO() runner = TestRunner(stream=stream) result = nose.run( testRunner=runner, argv=[sys.argv[0], module_name, '-s', '-v', ' collect-only' ] ) return runner.result.testsRun if __name__ == '__main__': print count_unit_tests('.')
您可以从python脚本运行any nose命令,如basic nose usage中所述,唯一的技巧是提取测试的数量。我看了一下functional tests in nose,觉得这样应该行得通,但您可以进一步缩小它:
相关问题 更多 >
编程相关推荐