Pytest 单元测试框架
1、pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效
2、安装 pytest
pip install pytest
3、验证 pytest 是否安装成功
pip show pytest
4、使用 pytest 执行测试需要遵行的规则
.py 测试文件必须以 test_ 开头(或者以 _test 结尾)
测试类必须以 Test 开头,并且不能有 init 方法
测试方法必须以 test_ 开头
断言必须使用 assert,pytest 中没有自带的断言方法
5、pytest 执行方式
- pytest –v filename(最高级别信息 — verbose)
- pytest -s filename(输出打印)
- pytest -v -s filename
- pytest -q filename(静默输出,不会打印用例输出)
6、实例介绍一
- dos 窗口中执行用例
# test_demo1.py def test_01():
print("深圳多测师") def test_02():
print("广州多测师") def test_03():
print("上海多测师")
- 执行方法一:pytest -q test_demo1.py

- 执行方法二:pytest -s test_demo1.py

- 执行方法三:pytest -v test_demo1.py

- 执行方法四:pytest -v -s test_demo1.py

7、实例介绍二
- demo1
- demo1 发现结果中没有用例的执行打印结果
# test_demo.py import pytest def test_01():
print("深圳多测师") def test_02():
print("广州多测师") def test_03():
print("上海多测师") if __name__ == '__main__':
pytest.main() # 结果如下
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: D:\work_doc\CodeFile\practice\pytest_demo
plugins: html-2.1.1, metadata-1.9.0
collected 3 items test_demo1.py ... [100%]
============================== 3 passed in 0.05s ==============================
Process finished with exit code 0
- demo2
- demo2 中在 main() 中加入 ["-s"],结果中就可以展示打印结果
import pytest def test_01():
print("深圳多测师") def test_02():
print("广州多测师") def test_03():
print("上海多测师") if __name__ == '__main__':
pytest.main(["-s"]) # 在 main() 中加入 ["-s"] # 结果如下
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: D:\work_doc\CodeFile\practice\pytest_demo
plugins: html-2.1.1, metadata-1.9.0
collected 3 items test_demo1.py 深圳多测师
.广州多测师
.上海多测师
.
============================== 3 passed in 0.02s ==============================
Process finished with exit code 0
- demo3
- 运行指定的用例文件
# test_demo1.py def test_01():
print("深圳多测师") def test_02():
print("广州多测师") def test_03():
print("上海多测师")
# run_all.py import pytest if __name__ == '__main__':
pytest.main(["D:/test_demo1.py"]) # 结果如下
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: D:\
plugins: html-2.1.1, metadata-1.9.0
collected 3 items test_demo1.py ... [100%]
============================== 3 passed in 0.01s ==============================
Process finished with exit code 0
- demo4
- 运行指定目录下的用例文件
# demo1/test_demo1.py demo1 目录下的 test_demo1.py 文件 def test_01():
print("深圳多测师") def test_02():
print("广州多测师") def test_03():
print("上海多测师")
# demo2/test_demo2.py demo2 目录下的 test_demo2.py 文件 def test_04():
print("杭州多测师") def test_05():
print("北京多测师") def test_06():
print("重庆多测师")
import pytest if __name__ == '__main__':
pytest.main(["D:/demo1","D:/demo2"]) # 指定 D:/demo1 和 D:/demo2 目录 # 结果如下
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: D:
plugins: html-2.1.1, metadata-1.9.0
collected 6 items demo1\test_demo1.py ... [ 50%]
demo2\test_demo2.py ... [100%]
============================== 6 passed in 0.04s ==============================
Process finished with exit code 0
8、pytest-html 生成测试报告
- 安装 pytest-html
pip install pytest-html
- cmd 下执行用例且生成报告

- 代码中生成测试报告
import pytest def test_01():
print("深圳多测师") def test_02():
print("广州多测师") def test_03():
print("上海多测师") if __name__ == '__main__':
pytest.main(["-q","test_demo1.py","--html=report.html"]) # --html=report.html 生成测试报告,也可以自定义报告路径如:--html=d://report.html

Pytest 单元测试框架的更多相关文章
- Pytest单元测试框架-测试用例运行规则
1.Pytest测试用例运行规则 在pytest单元测试框架下面执行用例,需要满足以下几个特点: 1. 文件名以test_*.py开头或者*_test.py 2. 测试类.测试函数以test开头 3. ...
- Pytest单元测试框架-Pytest环境安装
unittest是python自带的单元测试框架,它封装好了一些校验返回的结果方法和一些用例执行前的初始化操作,使得单元测试易于开展,因为它的易用性,很多同学也拿它来做功能测试和接口测试,只需简单开发 ...
- Pytest单元测试框架:插件-allure-pytest环境搭建并在本地生成一个测试报告
之前写了allure-pytest的官方文档啃的内容,有些交流的朋友,实践起来没什么头绪,所以就有了这篇文章,也给自己填个坑 第一步:搭建Allure.JDK环境 1. 搭建JDK环境 不装jdk你会 ...
- Pytest单元测试框架之简单操作示例
前言: Pytest是第三方单元格测试框架,更加简单,灵活,而且提供了更多丰富的扩展: Pytest与UnitTest框架的区别 UnitTest测试用例执行顺序是依照ascii码执行,而Pytest ...
- Pytest单元测试框架——Pytest+Allure+Jenkins的应用
一.简介 pytest+allure+jenkins进行接口测试.生成测试报告.结合jenkins进行集成. pytest是python的一种单元测试框架,与python自带的unittest测试框架 ...
- Pytest单元测试框架-学习
pytest: Python的一个单元测试框架,基于UnitTest二次开发,语法上更加简洁,可以用来做Python开发项目的单元测试,UI自动化.接口自动化测试等,有很多的插件访问Pytest插件汇 ...
- Pytest单元测试框架之FixTure基本使用
前言: 在单元测试框架中,主要分为:测试固件,测试用例,测试套件,测试执行及测试报告: 测试固件不难理解,也就是我们在执行测试用例前需要做的动作和测试执行后的需要做的事情: 比如在UI自动化测试中,我 ...
- pytest单元测试框架
一.安装方式 1.安装命令:pip install pytest 2.html安装插件:pip install pytest -html 二.pytest执行指定测试用例 1.思想:通过对测试用例进行 ...
- Pytest单元测试框架-allure测试报告
Allure Test Report 对于不同的编程语言,有很多很酷的测试框架.不幸的是,它们中只有少数能够提供测试执行输出的良好表示.Qameta软件测试团队正在致力于Allure--一个开源框架, ...
随机推荐
- 【Jenkins】插件更改国内源
最近调试脚本,本机安装了Jenkins,但是安装插件时一直失败.更改升级站点也不生效,究其原因是因为default.json中插件下载地址还https://updates.jenkins.io,升级站 ...
- windows批处理protoc生成C++代码
1 首先需要生成protoc的可执行文件,具体可以参考 https://www.cnblogs.com/cnxkey/articles/10152646.html 2 将单个protoc文件生成.h ...
- vue2.x学习笔记(十九)
接着前面的内容:https://www.cnblogs.com/yanggb/p/12631022.html. 程序化的事件侦听器 在前面的学习中,我们已经知道了[$emit]全局属性的用法,它可以被 ...
- beanshell 常用的内置变量与函数
官方详细文档:https://github.com/beanshell/beanshell/wiki log:用来记录日志文件 log.info("jmeter"); vars - ...
- BIOS时间与系统时间(windows/linux时间同步问题)
写作动机 双系统是不少人喜欢的方式,但安装双系统之后一般会出现两个系统时间不一样的问题,刚开始用双系统的时候也没怎么在意,就是装上后在网上找找相关解决方法,复制粘贴代码完事儿.但是次数多了就有点烦了, ...
- Java中什么是构造方法
this(...)本类的构造方法super(...)父类的构造方法构造方法:给对象的数据进行初始化格式:A:方法名与类名相同B:没有返回值类型,连void都没有C:没有具体的返回值注意事项:A:如果我 ...
- gin请求数据校验
前言 最近优化gin+vue的前后端分离项目代码时候,发现代码中对请求数据的校验比较繁琐,于是想办法简化它.最终我发现了go-playground/validator开源库很好用. 优化前代码 代码如 ...
- 2019-2020-1 20199325《Linux内核原理与分析》第十一周作业
实验简介: Set-UID 是 Unix 系统中的一个重要的安全机制.当一个 Set-UID 程序运行的时候,它被假设为具有拥有者的权限.例如,如果程序的拥有者是root,那么任何人运行这个程序时都会 ...
- QT 获取可执行程序的路径
获取到生成.exe目录 QCoreApplication::applicationDirPath(); 获取当前工程目录 QDir::currentPath()
- Web性能优化之-深入理解TCP Socket
什么是Socket? 大家都用电脑上网,当我们访问运维社区https://www.unixhot.com的时候,我们的电脑和运维社区的服务器就会创建一条Socket,我们称之为网络套接字.那么既 ...