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--一个开源框架, ...
随机推荐
- 数据结构之栈—强大的四则复杂运算计算器(超过windows自带的科学计算器)【中缀转后缀表达式】
比windows自带计算器还强的四则复杂运算计算器! 实测随机打出两组复杂算式:-7.5 * 6 / ( -2 + ( -6.5 - -5.22 ) )与7.5+-3*8/(7+2) windows ...
- A. Number Theory Problem
题目大意:计算小于2^n,且满足2^k-1并且是7的倍数的个数 思路:优先打表,数据不大,1e5,然后求个前n项和 #include<bits/stdc++.h> using namesp ...
- Robberies 杭电
可怜的POIUYTREWQ最近想买下dota2的商品,但是手头缺钱.他想起了之前看过的一部大片,觉得抢银行也许是个不错的选择.他认为,坏人被抓是因为没有预先规划.于是他在之前的几个月对各大银行进行了一 ...
- Gatling脚本编写技巧篇(二)
脚本示例: import io.gatling.core.Predef._ import io.gatling.http.Predef._ import scala.concurrent.durati ...
- Hadoop的下载和安装
Hadoop的下载和安装 一:Hadoop的简介 Apache的Hadoop是一个开源的.可靠的.可扩展的系统架构,可利用分布式架构来存储海量数据,以及实现分布式的计算. Hadoop许使用简单的编程 ...
- JavaScript--'data-'的用法(1)
HTML5为我们提供了一个强大的功能,前段也也能实现后台数据库的效果,例如data-xxx <a href="#myModal" data-industry_id=" ...
- 元素均匀排列自动换行&二维数组前端遍历
1.元素均匀排列并自动换行 display:flex; flex-wrap:wrap; 2.getFiled();取一行,取多行的话用getFiled(‘id’,true); 3.二维数组前端遍历: ...
- js的属性监听
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title ...
- Python封装应用程序的最佳项目结构是什么?
Python封装应用程序的最佳项目结构是什么? 转载来源于stackoverflow:https://stackoverflow.com/questions/193161/what-is-the-be ...
- Nginx入门及如何反向代理解决生产环境跨域问题
1.Nginx入门与基本操作篇 注:由于服务器是windows系统,所以本文主要讲解Nginx在windows下的操作. 首先下载Nginx 解压缩,我们所有的配置基本都在万能的 nginx/conf ...