我使用XMLTestRunner从Python运行单元测试用例。在
这是我的主要文件:
#LoggingService.py
class LoggerWriter:
# used to redirect console msgs to the log
def write(self, message):
log_event('warning', 'Console', message)
def direct_console_to_log():
global console_redirected
# This function can be called several times. Avoid redirecting more than once
if not console_redirected:
console_output = LoggerWriter()
sys.stderr = console_output
sys.stdout = console_output
console_redirected = True
def log_event(level, _id, event_details):
# log the event
pass
这是我的一个测试文件:
^{pr2}$我曾经运行过所有的测试文件XMLRunner.py公司名称:
#XMLRunner.py
from xmlrunner import XMLTestRunner
import unittest
test_modules = [file1.Test, file2.Test, LoggingServiceTest.py, .... file25.Test]
suite = unittest.TestSuite()
for test_module in test_modules:
tests = unittest.TestLoader().loadTestsFromTestCase(test_module)
suite.addTests(tests)
if __name__ == '__main__':
# import xmlrunner
# unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))
XMLTestRunner(output='test-reports').run(suite)
当我想跑的时候LoggingServiceTest.py文件来自“XMLRunner.py,则测试报告没有生成。但是当我尝试没有“LoggingServiceTest.py,则报告生成成功。在
我不确定测试用例函数发生了什么 模拟自测(日志)LoggingServiceTest.py
我使用以下命令运行测试用例:
Python2.7.14\python.exe -m coverage run -p --branch --source=. --omit=Unit_Tests/*.py Unit_Tests/Testing.py
最后我在调试了更多之后得到了解决方案。在
当从XMLRunner启动测试用例时,它正在为编写stderr和stdout创建一些文件对象,并且它是在
sys.stderr, sys.stdout
变量中定义的我的测试用例是覆盖
sys.stderr, sys.stdout
变量。在因此XMLRunner尝试从
sys.stderr, sys.stdout
获取文件对象,但它不在那里,因此它不会生成报告。在相关问题 更多 >
编程相关推荐