pytho接口测试,生成测试报告

python批量执行测试用例后如何生成测试报告部分

python没有测试报告的模版,可以通过下面的地址,下载HTMLTestRunner模块

http://tungwaiyip.info/software/HTMLTestRunner.html

如下图截图,将html模块保存到本地:

  脚本下载完成,可以放在测试文件路径下,也可以放在python27,lib文件夹下。我的是python27版本

runcase.py

# -*- coding: utf-8 -*-
#! /usr/bin/env python
#
# This file is part of pySerial - Cross platform serial port support for Python
# (C) 2001-2015 Chris Liechti <cliechti@gmx.net>
#
# SPDX-License-Identifier: BSD-3-Clause
"""
UnitTest runner. This one searches for all files named test_*.py and collects
all test cases from these files. Finally it runs all tests and prints a
summary.
"""
import sys

import os


import sys
import imp
imp.reload(sys)

sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
# sys.path.append('../')

import unittest

import HTMLTestRunner
from common.project_path import test_report_path

# 输出当前文件的路径到控制台
print os.path.splitext(__file__)[0]

# find files and the tests in them
# 将测试套件实力化
mainsuite = unittest.TestSuite()


# 导出当前路径下的以test开头.py结尾的文件,
for modulename in [
os.path.splitext(x)[0]
for x in os.listdir(os.path.dirname(__file__) or '.')
if x != __file__ and x.startswith("test") and x.endswith(".py")
]:

try:
module = __import__(modulename)
except ImportError:
print("skipping %s" % (modulename))
else:
     # findTestCases模块,找出测试文件中的测试用例,以test开头的函数代表一个测试用例

testsuite = unittest.findTestCases(module)
print("found %s tests in %r" % (testsuite.countTestCases(), modulename))
# addTest 将测试用例加入测试套件里面
     mainsuite.addTest(testsuite)

print('-' * 78)

# HTMLTestRunner里的run方法,执行测试用例生成测试报告

with open(test_report_path, "wb+") as file:
runner = HTMLTestRunner.HTMLTestRunner(file, title='TestReport', description='SoftWareTest')
runner.run(mainsuite)


# verbosity = 2
# run the collected tests
# testRunner = unittest.TextTestRunner(verbosity=verbosity)
#~ testRunner = unittest.ConsoleTestRunner(verbosity=verbosity)
# result = testRunner.run(mainsuite)

# sys.exit(not result.wasSuccessful())

 



原文地址:https://www.cnblogs.com/lin-yue/p/10913779.html