python - 单元测试记录结果文件

标签 python logging python-unittest

我需要记录到控制台中显示的文件测试结果: '在 83.904 秒内运行 1 次测试 好的 '

我的方法:

 def allTests(self):
    testmodules = [
        #'tests.tests1',
        'tests.tests2',
        'tests.tests3'    
    ]
    suite = unittest.TestSuite()
    results = unittest.TextTestRunner(verbosity=2).run(suite)
    for t in testmodules:
        suite.addTest(unittest.defaultTestLoader.loadTestsFromName(t))
        print results
    unittest.TextTestRunner().run(suite)

我运行我的测试:

if __name__ == '__main__':
timestr = time.strftime("%d%m%Y-%H%M%S")
logFormatter = log.Formatter("%(asctime)s [%(levelname)s]  %(message)s")
rootLogger = log.getLogger()
logPath = './Logs/'
fileName = '{0}--StartTests'.format(timestr)
fileHandler = log.FileHandler("{0}/{1}.log".format(logPath, fileName))
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)
consoleHandler = log.StreamHandler(sys.stdout)
rootLogger.setLevel(log.NOTSET)
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)
log.disable(log.DEBUG)
start = startTests()
start.allTests()

除了有关最终结果的信息(正常,跳过失败)之外,我已记录所有信息

在 72.710 秒内运行 1 次测试 失败(失败=1)

如何添加此信息???

请帮助我,谢谢。

最佳答案

它有效 我添加到:unittest.TextTestRunner().run(suite) >>> log.info(unittest.TextTestRunner().run(suite))

关于python - 单元测试记录结果文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51825039/

相关文章:

java - 记录时使用同步

python - 气刹 throw 错误 "pybrake - ERROR - strconv.ParseInt: parsing "无“: invalid syntax"

python - Unittest 子测试中的局部变量未定义

python - 如何比较两个文件作为单元测试的一部分,同时在不匹配的情况下获得有用的输出?

python - 如何将非分组列保留在 Pandas 的 groupBy 中

python - 将 Python 脚本作为 Windows 后台进程运行

c# - .NET : Logging entry/exit without AOP?

Python Mock Patch 一个类中的多个方法

python - Django "No Patterns"/循环导入错误

python - python中word的所有同义词?