我创建了一个日志类:
import logging, time
class QaLogger():
def __init__(self, filename='LOG.log', logger_name='Qa_Automation'):
logging.basicConfig(filename=filename, level=logging.INFO)
self.logger = logging.getLogger(logger_name)
self.logger.initialized = True
def log(self, msg):
localtime = time.localtime()
time_string = time.strftime("%Y-%m-%d-%H:%M:%S", localtime)
self.logger.info(time_string + ": " + msg)
然后我在运行测试时使用它来将输出记录到文件中: 示例:
self.logger = QaLogger('qa_req_response.log', 'QA')
self.logger.log('QA_LOGGING ')
当我使用 PyCharm IDE 运行我的测试时,这工作正常;登录文件已完成。
我的问题是,当我从命令行使用 nosetests.exe 运行单元测试时它不起作用,例如:
> C:\Python27\Scripts\nosetests.exe .\TestFunction.py --with-xunit
有或没有--with-xunit
日志记录未完成,日志文件仍为空。
我该如何解决这个问题?
最佳答案
尝试在命令行上使用 --nologcapture
选项。如果我没记错的话,Nose 的 logcapture 插件默认会拦截所有日志记录,这导致 basicConfig
没有达到预期的效果。
关于使用 nosetests.exe 的 Python 单元测试阻止使用日志记录模块登录文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30009216/