python interface to automatic test generation of test reports -unittest-

After resolving the problem with the management of cases, the next thing to consider is to report my problem, generate test reports here HTMLTestRunner.py main use of this module, the following briefly explain how to use:

First, download HTMLTestRunner download:

This module can not be installed by pip, only download and install, download the following address:

 

Second, under mac configuration:

1, the terminal environment into the python

2, type:

import sys
print sys.path

 

3, find the path site-packages folder and download  HTMLTestRunner.py  files are copied to this folder under 
4, in the python environment, i.e., no error input import HTMLTestRunner successful installation
 
 
Third, the use of the report generation module:
1, the directory structure
  • package case below baidu, httpbin two packets
  • Each packet has the following two tests are py file
  • Each test_00x.py files have two test case
  • run_all_case.py file: used to perform all the test case and test report generation
  

2, after generating run as follows:

 

3, run_all_case.py code is as follows:

复制代码
# -*- coding:utf-8 -*-
import unittest
import os
import time
import HTMLTestRunner

# 用例路径
case_path = os.path.join(os.getcwd())
# 报告存放路径
report_path = os.path.join(os.getcwd(), 'report')
print report_path

def all_case():
    discover = unittest.defaultTestLoader.discover(case_path, pattern="test*.py", top_level_dir=None)

    print discover
    return discover

if __name__ == '__main__':
    # 1、获取当前时间,这样便于下面的使用。
    now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))

    # 2、html报告文件路径
    report_abspath = os.path.join(report_path, "result_"+now+".html")

    # 3、打开一个文件,将result写入此file中
    fp = open(report_abspath, "wb")
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp,
                                           title=u'接口自动化测试报告,测试结果如下:',
                                           description=u'用例执行情况:')
    # 4、调用add_case函数返回值
    runner.run(all_case())
    fp.close()

Guess you like

Origin www.cnblogs.com/jiachangwei/p/12153628.html