"Dove into the" series of Python + Selenium automated testing framework combat chapter 7-- not the end of the promotion and pay rise, year-end bonus depends on it! ! !

1 Introduction

  As of the end of last article, the basic framework is built entirely completed. So today what we have to do it? ? ? ? Smart as your little partner or shoes must have guessed, are finished testing, of course, is to generate a test report of a high-end atmosphere on the grade of the. Yes, the macro brother today to lead this part of your content is also integrated into this framework. This article describes how to generate automated test report, referred to in previous articles tail HTMLTestRunner.py generated using automated testing report. About HTMLTestRunner however much introduction, just need to know that a web page can generate an HTML report formats can be other if you are particularly interested can go search query data. Automation framework in which we need to introduce the macro front brother inherits a report output directly to see the effect.

2. Test results report

2.1 Test Report File

  From the figure we can clearly see the test report generation in test_report folder

2.2 browser preview

  Because .html files are generated, so here opened with a browser's look at the following picture:

2.3 realized

  Here we report the output code is written, and is responsible for implementing the test suite TestRunner.py this file. Related code is as follows:

TestRunner.py

2.3.1 code implementation:

2.3.2 Reference Code:
# -*- coding:utf-8 -*-

# 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行

# 2.注释:包括记录创建时间,创建人,项目名称。
'''
Created on 2019-12-25
@author: 北京-宏哥   QQ交流群:705269076
Project: 《一头扎进》系列之Python+Selenium自动化测试框架实战篇7 - 年底了还没升职加薪,年终奖全靠它了!!!
'''
# 3.导入模块
from automation_framework_demo.testsuits import HTMLTestReportCN
import os
import unittest
import time
from automation_framework_demo.framework.SendEmail import SendMail

# 设置报告文件保存路径
report_path = os.path.dirname(os.path.abspath('.')) + '/test_report/'

print('report_path'+ report_path)
# 获取系统当前时间
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))

# 设置报告名称格式
HtmlFile = report_path + now + "HTMLtemplate.html"
#fp = file(HtmlFile, "wb")
fp = open(HtmlFile, "wb")

#用例路径
case_path = os.path.join(os.getcwd(), '')
print('case_path',case_path)


# 构建suite
# suite = unittest.TestLoader().discover(case_path, "test_baidu_search.py", top_level_dir=None)
suite = unittest.TestLoader().discover(case_path, "baidu_search1.py", top_level_dir=None)

if __name__ == '__main__':
    #初始化一个HTMLTestRunner实例对象,用来生成报告
    runner = HTMLTestReportCN.HTMLTestRunner(stream=fp, title=u"Python+Selenium自动化测试框架实战篇7项目演示测试报告", description=u"用例测试情况")
    #开始执行测试套件
    runner.run(suite)
    # runner = unittest.TextTestRunner()
    # runner.run(suite)
    fp.close()
2.3.3 运行结果:

  运行代码后,控制台打印如下图的结果

2.3.4 测试报告预览

  测试报告用浏览器打开截图,在你项目文件路径下,找到这个HTML的报告,用本地浏览器打开。

  如何生成自动化测试报告就介绍到这里。

3. 测试报告模板插件

  宏哥这里为了方便,直接将测试报告的模板插件,直接放在了testsuits的文件下,如图:

  如果有洁癖的小伙伴或者童鞋们,可以自己建一个文件夹专门来存储这个模板插件也可以的,但是不要忘记了最重要的一条就是将其导入,否则可能会报找不到的错误哦!

4. 导入测试报告模板插件

  如何导入?其实很简单的,就像python导入其他模块一样。例如宏哥这里的导入方法如下:

  宏哥总结如下:

from  文件夹路径  import 插件名字.py

5. 执行所有测试用例

  除了上一篇那种找到文件夹,执行所有测试用例,我们还有别的方法执行所有测试用例吗?当然有,但是对测试用例的名字的命名有要求,其实也不是硬性要求,只不过是为了后边我们的正则表达式好写,这里命名就规范一些。

5.1 命名规则:

  所有测试用例的名字都以test开头,例如:test_login.py。

5.2 正则表达式:

  正则表达式:test*.py

5.3 代码实现:

5.4 参考代码:

# 构建suite
# suite = unittest.TestLoader().discover(case_path, "test_baidu_search.py", top_level_dir=None)
suite = unittest.TestLoader().discover(case_path, "test*.py", top_level_dir=None)

6. 小结

  好了,今天的分享就到这里吧!!!谢谢各位的耐心阅读。有问题加群交流讨论

 

您的肯定就是我进步的动力。如果你感觉还不错,就请鼓励一下吧!记得随手点波  推荐  不要忘记哦!!!

别忘了点 推荐 留下您来过的痕迹

 

 

Guess you like

Origin www.cnblogs.com/du-hong/p/12095160.html