源代码如下(源代码本为python3.5.6版本,后面复制到python 2.7版本上运行):
from HTMLTestRunner import HTMLTestRunner import unittest import os import time if __name__=='__main__': case_path = os.path.join(os.getcwd()) print case_path root_path = os.path.abspath(os.path.dirname(os.getcwd())) print root_path report_path = os.path.join(root_path, 'report') print report_path now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) report_path1 = os.path.join(report_path, 'autotest_result_' + now + '.html') report_file = open(report_path1, "wb") runner=HTMLTestRunner(stream=report_file,title=u'自动化测试报告',description=u'测试结果') cases = unittest.defaultTestLoader.discover(case_path, pattern='test*.py', top_level_dir=None) runner.run(cases) report_file.close()
运行以上代码的时候,出现报错:SyntaxError: Non-ASCII character '\xe8'
后来发现用python2.7版本的时候必须在最开头加上:
# - *- encoding: utf- 8 - *-
如果只有# - *- encoding: utf- 8 - *-,没有在中文前面加 u 也是会出现报错的,报错具体如下:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 211: ordinal not in range(128)