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--一个开源框架, ...
随机推荐
- 你的网购价格监督利器——python+爬虫+微信机器人
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:风,又奈何 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...
- work of 1/6/2016
part 组员 今日工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云 UI动态布局改进和攻克疑难 6 继续下滑条等增删补减 ...
- Daily Scrum 12/25/2015
Process: Zhaoyang: Implement the Alex 50M model in the Caffe framework. Yandong: The Azure Back end ...
- 从头学pytorch(十三):使用GPU做计算
GPU计算 默认情况下,pytorch将数据保存在内存,而不是显存. 查看显卡信息 nvidia-smi 我的机器输出如下: Fri Jan 3 16:20:51 2020 +------------ ...
- JavaScript--'data-'的用法(1)
HTML5为我们提供了一个强大的功能,前段也也能实现后台数据库的效果,例如data-xxx <a href="#myModal" data-industry_id=" ...
- Centos史上新版最详细步骤-Linux无脑命令式oracle11g静默安装
1. 关闭selinux 1.1 sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config 1.2 或者 ...
- Memcached在企业中的应用
Memcached简介 Memcached是一个自由开源的,高性能,分布式内存对象缓存系统. Memcached是以LiveJournal旗下Danga Interactive公司的Brad Fitz ...
- Spring5参考指南:Bean的生命周期管理
文章目录 Spring Bean 的生命周期回调 总结生命周期机制 startup和Shutdown回调 优雅的关闭Spring IoC容器 Spring Bean 的生命周期回调 Spring中的B ...
- Libra教程之:执行Transactions
文章目录 Transactions是什么 Transactions运行的基础条件 Transactions的结构 执行Transactions Transactions是什么 我们讲到了Libra是一 ...
- Libra教程之:来了,你最爱的Move语言
文章目录 Move语言 Move的核心概念 Move交易脚本 Move modules Move resources 写一个Move程序 编写交易脚本 编写自己的Modules Move语言 Move ...