一份简单的测试报告

一份简单的测试报告可以使用pytest的插件html就可以生成, demo如下 先下载

pip install pytest-html

下载完之后,在当前执行过测试用例的测试目录下的终端执行

pytest --html=report.html # 即可生成一份简单的 report.html 报告, 如下图

allure介绍

allure 可以生成一个漂亮的测试报告。做kpi的时候老板看到可以加个鸡腿

allure安装

allure安装还是有点复杂的,首先是给到allure的下载路径 https://github.com/allure-framework/allure2/releases

下载完成之后,解压到本地电脑,这步没问题就要

把bin目录添加到环境变量Path下

bin目录

然后我的电脑右键属性 -> 高级设置 -> 环境变量path编辑

这步完成代表基本可以,但是电脑如果没有安装jdk的情况下,allure是运行不成功的。所以我们又要去安装一个jdk

安装jdk参考 https://blog.csdn.net/weixin_44535573/article/details/110232317

jdk安装完毕之后,就可以在终端中输入

allure --version

这样就可以使用allure服务了

pytest_allure 插件 Allure报告生成

安装完allure 如果需要在python中使用allure,则需要安装多一个库来操作allure

allure官方文档 参考:https://docs.qameta.io/allure/#_pytest

pip install allure-pytest —index-url https://pypi.douban.com/simple

参考官网的demo,生成一个简单的例子

新建一个文件

import pytest

def test_success():
"""this test succeeds"""
assert True def test_failure():
"""this test fails"""
assert False def test_skip():
"""this test is skipped"""
pytest.skip('for a reason!') def test_broken():
raise Exception('oops') if __name__ == '__main__':
pytest.main(["-s", "test_allure01.py"])

在该文件夹下创建多一个result文件夹,然后在终端执行命令

pytest .\test_allure01.py --alluredir=result/1 # 将文件生成在result下的1文件

生成成功后会有一批json的数据,这时候我们就可以用allure来执行这个文件夹

allure serve .\result\1\

还有一种方法是生成在本地的,有兴趣可以自行了解

allure 特性分析

场景:

  • 希望在报告中看到测试功能,子功能或者场景,测试步骤 包括测试附加信息

    解决:
  • @Feature, @story, @step, @attach

    步骤:
  • import allure
  • 功能上加 @allure.feature("功能名称")
  • 子功能上加 @allure.story("子功能名称")
  • 步骤上加 @allure.step("步骤细节")
  • allure.attach("具体文本信息"), 需要附加的信息,可以是数据,文本,图片,视频,网页

新建一个文件demo

import pytest
import allure @allure.feature("登录模块")
class TestLogin():
@allure.story("登录成功")
def test_login_success(self):
print("这是登录")
pass @allure.story("登录失败")
def test_login_success_a(self):
print("这是登录")
pass @allure.story("用户名缺失")
def test_login_success_b(self):
print("用户名缺失")
pass @allure.story("密码缺失")
def test_login_failure(self):
with allure.step("点击用户名"):
print("输入用户名")
with allure.step("点击密码"):
print("输入密码")
with allure.step("点击登录之后登录失败"):
assert '1' == 1
print('登录失败')
pass @allure.story("登录失败")
def test_login_failure_a(self):
print("这是登录")
pass if __name__ == '__main__':
pytest.main()

然后用allure生成,看看生成的不同, 就知道用法的差别

如果有很多用例在,而只想运行一个模块用例的话, 那就运行

pytest .\test_allure02.py --allure-features '登录模块'

或者 只想执行某个模块下的story

pytest -v .\test_allure02.py --allure-stories '登录成功'
  • feature相当于一个功能,一个大的模块,将case分类到某个feature中,story属于feature下的内容
  • feature与story类似于父子关系
  • @allure.step() 只能以装饰器的形式放在类或者方法上面,用于关键点
  • @allure.severity(allure.severity_level.TRIVIAL) 这是allure的标记,执行时 pytest -s -v 文件名 --allure-severities normal, critical 这样就可以执行相关的优先级
  • allure.attach()

allure + pytest + selenium实战

默认已经调好selenium

实战内容为通过selenium 搜索三个关键词然后截图放到allure中, 前端自动化测试 百度搜索功能实战演示

import allure
import pytest
from selenium import webdriver import time # chrome_options = webdriver.ChromeOptions()
@allure.testcase("##") # 默认连接到一个测试用例管理的地址
@allure.feature('百度搜索')
@pytest.mark.parametrize('test_data1', ['allure', 'unitest', 'pytest'])
def test_step_demo(test_data1):
with allure.step("打开百度网页"):
driver = webdriver.Chrome(r'D:\my_project\git_deve\development\python_work\chromedriver_win32\chromedriver.exe')
driver.get('http://www.baidu.com')
driver.maximize_window() with allure.step(f"输入搜索词{test_data1}"):
driver.find_element_by_id('kw').send_keys(test_data1) # 找到百度的Kw, 然后输入你好世界
time.sleep(2)
driver.find_element_by_id('su').click()
time.sleep(2) with allure.step("保存图片"):
driver.save_screenshot('./result/1/a.png')
allure.attach.file('./result/1/a.png', attachment_type=allure.attachment_type.PNG)
allure.attach('<head></head><body>首页</body>', 'Attach with HTML type', allure.attachment_type.HTML)
with allure.step("退出浏览器"):
driver.quit()

代码写完之后。然后执行

pytest .\test_baidudemo.py --alluredir=result/1
# 然后生成报告
allure serve .\result\1

可以再改写一下,不用连续的打开chrom

import allure
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options import time # chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222") # 指定配置好的 chrom
# chrome_driver = r"E:\\chromedriver.exe" # 驱动路径
# driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options) # 加入驱动设置
# chrome_options = webdriver.ChromeOptions() @allure.testcase("##") # 默认连接到一个测试用例管理的地址
@allure.feature('百度搜索')
@pytest.mark.parametrize('test_data1', ['allure', 'unitest', 'pytest'])
def test_step_demo(test_data1):
with allure.step("打开百度网页"):
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222") # 指定配置好的 chrom
chrome_driver = r"D:\my_project\git_deve\development\python_work\chromedriver_win32\chromedriver.exe" # 驱动路径
driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options) # 加入驱动设置
driver.get('http://www.baidu.com')
driver.maximize_window() with allure.step(f"输入搜索词{test_data1}"):
driver.find_element_by_id('kw').send_keys(test_data1) # 找到百度的Kw, 然后输入你好世界
time.sleep(2)
driver.find_element_by_id('su').click()
time.sleep(2) with allure.step("保存图片"):
driver.save_screenshot('./result/1/a.png')
allure.attach.file('./result/1/a.png', attachment_type=allure.attachment_type.PNG)
allure.attach('<head></head><body>首页</body>', 'Attach with HTML type', allure.attachment_type.HTML) with allure.step("退出浏览器"):
driver.quit()

完。

allure测试报告美化与定制的更多相关文章

  1. allure测试报告

    首先如果你没有安装 pytest 库的话,先使用 pip 安装一下: pip install pytest 另外还需要安装 pytest 支持 allure 报告的插件库: pip install a ...

  2. Python2 HTMLTestRunner自动化测试报告美化

    python2 的测试报告美化,需要的同学直接用 #coding=utf-8 """ A TestRunner for use with the Python unit ...

  3. Jenkins集成allure测试报告

    前言 Allure框架是一个功能强大的自动化测试报告工具,不仅支持多种编程语言,而且能够完美的与各种集成工具结合,包括Jenkins,TeamCity,Bamboo,Maven等等,因此受到了很多测试 ...

  4. Ubuntu全方位美化,定制教程

    Ubuntu全方位美化,定制教程 上一篇随笔聊了聊Linux图形界面的各种名词及其关系,解释了何为xserver,何为xclient,linux的图形界面是如何工作的,Linux图形软件的多样性.li ...

  5. Allure测试报告完整学习笔记

    目录 简介 安装Allure Allure测试报告的结构 Java TestNG集成Allure Report Python Pytest集成Allure Report 简介 假如你想让测试报告变得漂 ...

  6. HTMLTestRunner测试报告美化

    前言 ​最近小伙伴们在学玩python,,看着那HTMLTestRunner生成的测试报告,左右看不顺眼,终觉得太丑.搜索了一圈没有找到合适的美化报告,于是忍不住自已动手进行了修改,因习惯python ...

  7. Pytest单元测试框架-allure测试报告

    Allure Test Report 对于不同的编程语言,有很多很酷的测试框架.不幸的是,它们中只有少数能够提供测试执行输出的良好表示.Qameta软件测试团队正在致力于Allure--一个开源框架, ...

  8. TestNG测试报告美化

    因TestNG自带的测试报告不太美观,可以使用testng-xslt进行美化 1.下载testng-xslt包 2.把/src/main/resources/TestNG-results.xsl放到你 ...

  9. java TestNG测试报告美化

    测试报告 执行完测试用例之后,会在项目的test-output(默认目录)下生成测试报告 打开index.html文件,测试结果摘要,包括:套件名.测试用例成功数.测试用例失败数.测试用例忽略数和te ...

  10. Python3 HTMLTestRunner自动化测试报告美化

    # FileName : MyHTMLTestRunner.py # Author : wangyinghao # DateTime : 2019/1/9 21:04 # SoftWare : PyC ...

随机推荐

  1. Windows下Zookeeper安装使用

    Windows下Zookeeper安装使用 ZooKeeper是一种分布式协调服务,用于管理大型主机. 在分布式环境中协调和管理服务是一个复杂的过程. ZooKeeper通过其简单的架构和API解决了 ...

  2. .DS_Store泄露利用过程

    .DS_Store文件泄漏利用工具: ds_store_exp.DS_Store是Mac下Finder用来保存如何展示 文件/文件夹 的数据文件,每个文件夹下对应一个. 如果开发/设计人员将.DS_S ...

  3. vite不能用@做为路径的解决方法

    vite创建vue3后,发现原来用@做为路径的不能用了,报错信息是 Internal server error: Failed to resolve import "@ 在网上查了一下资料, ...

  4. 脏牛-Linux内核提权

    漏洞范围 下载地址:https://github.com/FireFart/dirtycow 有一点可以在意,dirty.c内置在了kali中,使用命令searchsploit dirty可以搜索 也 ...

  5. 【CS231n assignment 2022】Assignment2 - Part 1,全连接网络的初始化以及正反向传播

    文章目录 前言 FullyConnectedNets 网络初始化 代码注意事项 1. W 的尺寸怎么知道? 2. np.random.norml() 3. batchnorm loss(self, X ...

  6. Navicate 远程连接Mysql数据库

    123步骤是在服务器上运行,第四步是在本机的navicate上运行 1.use mysql 2.update user set host = '%' where user = 'root' 3.flu ...

  7. 常用Linux发行版操作系统大盘点

    B站CodeSheep的教程 https://www.bilibili.com/read/cv6026694

  8. ThreadLocal 类

    更多内容,访问 IT-BLOG ThreadLocal 并不是一个Thread,而是 ThreadLocalVariable(线程局部变量).也许把它命名为 ThreadLocalVar更加合适.线程 ...

  9. python协程详细解释以及例子

    目录 1.协程 1.1greenlet实现协程 1.2yield关键字实现协程 1.3使用asyncio模块实现协程 1.4async & await关键字实现协程 2.协程意义 3.异步编程 ...

  10. Excel或数据库快速生成GUID

    一般一些开发软件或者网站可以直接生成guid, 比如:https://www.iamwawa.cn/guid.html 但是在某些场景下,经常在一些excel或者数据库操作需要快速生成指定格式的gui ...