当前位置:网站首页>An article to teach you a quick start and basic explanation of Pytest, be sure to read
An article to teach you a quick start and basic explanation of Pytest, be sure to read
2022-08-10 22:00:00 【Rejoice in the testing world】
前言
目前有两种纯测试的测试框架,pytest和unittest
unittest应该是广为人知,而且也是老框架了,很多人都用来做自动化,无论是UI还是接口
pytest是基于unittest开发的另一款更高级更好用的单元测试框架
Go out for an interview,It's good to talk to others,pytestis significantly higher than that of unittest
为什么要用Pytest
pytest 的官方网站介绍,它具有如下特点:
- 非常容易上手,入门简单,文档丰富,文档中有很多实例可以参考
- 能够支持简单的单元测试和复杂的功能测试
- 支持参数化
- 执行测试过程中可以将某些测试跳过(skip),或者对某些预期失败的case标记成失败
- 支持重复执行(rerun)失败的 case
- 支持运行由 nose, unittest 编写的测试 case
- 可生成 html 报告
- Convenient sustainable integration tool jenkins 集成
- 可支持执行部分用例
- 具有很多第三方插件,并且可以自定义扩展

安装Pytest
cmd运行
pip install -U pytestpip3 install pytest -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
查看版本
pytest --version
快速开始
#!/usr/bin/env python# -*- coding: utf-8 -*-"""__title__ =__Time__ = 2020-04-06 12:33__Author__ = 小菠萝测试笔记__Blog__ = https://www.cnblogs.com/poloyy/"""def func(x): return x + 1def test_answer(): assert func(3) == 5class TestClass: def test_one(self): x = "this" assert "h" in x def test_two(self): x = "hello" assert hasattr(x, "check")
然后,cmd进入当前文件目录,直接执行
pytest

知识点
如果只执行 pytest ,会查找当前目录及其子目录下以 test_*.py 或 *_test.py 文件,找到文件后,在文件中找到以 test 开头函数并执行
如果只想执行某个文件,可以 pytest start.py
加上-q,就是显示简单的结果: pytest -q start.py
Pytest用例的设计原则
用Pytest写用例时候,一定要按照下面的规则去写,否则不符合规则的测试用例是不会执行的
文件名以 test_.py 文件和test.py
以 test 开头的函数
以 Test 开头的类,不能包含 init 方法
以 test_ 开头的类里面的方法
所有的包 package 必须要有__init__.py 文件
Pytest执行用例规则
注意,The following are all incmd中执行pytest命令
1、All use cases in a directory
pytest
2、执行某一个 py 文件下用例
pytest 脚本名称.py
3、运行start.py 模块里面的某个函数,或者某个类,method in a class
加v和不加-v都可以,加-v的话,打印的信息更详细
pytest -v 08_mark.py::TestClass::test_methodpytest 08_mark.py::TestClass::test_methodpytest start.py::test_answer
4、运行start.py 模块里面,测试类里面的某个方法
pytest start.py::TestClass::test_two

5、-m 标记表达式(后续讲解)
pytest -m login
将运行用 @pytest.mark.login 装饰器修饰的所有测试,I'll expand on the markup later
6、-q 简单打印,只打印测试用例的执行结果
pytest -q start.py
7、-s 详细打印
pytest -s start.py
8、-x 遇到错误时停止测试
pytest start.py -x
9、—maxfail=num,当用例错误个数达到指定数量时,停止测试
pytest start.py --maxfail=1
10、-k 匹配用例名称
执行测试用例名称包含http的所有用例
pytest -s -k http start.py
11、-k Exclude some use cases based on the use case name
1 pytest -s -k "not http" start.py
12、-k 同时匹配不同的用例名称
pytest -s -k "method or weibo" start.py
Pycharm运行Pytest
平时写代码,we are all therePycharm写的,How could it be used all the timecmdLet's run the use case,现在我们就来看看在Pycharm中如何运行Pytest
1、首先,我们先要去settings里面设置单元测试框架为Pytest
2、如果是nosetests的话,Right click and run yespythonOh the script works
3、如果设置了unittest则是以unittest框架去运行
注意
pytest 是可以兼容 unittest 脚本的,之前写的 unittest 用例也能用 pytest 框架去运行
最后: You can get a copy yourself at the bottom216页软件测试工程师面试宝典文档资料【免费的】.以及相对应的视频学习教程免费分享!,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等.
喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一 键三连哦!
软件测试工程师自学教程:
这才是2022最精细的自动化测试自学教程,我把它刷了无数遍才上岸字节跳动,做到涨薪20K【值得自学软件测试的人刷】
软件测试工程师月薪2W以上薪资必学技能 — Python接口自动化框架封装.
美团面试真题_高级测试25K岗位面试 — 软件测试人都应该看看
软件测试必会_Jmeter大厂实战 — 仅6步可实现接口自动化测试

边栏推荐
- Live Classroom System 09--Tencent Cloud VOD Management Module (1)
- LeetCode每日一题(1573. Number of Ways to Split a String)
- JVM经典五十问,这下面试稳了
- Redis Performance Impact - Asynchronous Mechanisms and Response Latency
- F. Binary String Reconstruction
- [SQL brush questions] Day3----Special exercises for common functions that SQL must know
- About DataFrame: Processing Time
- Application of Spatial 3D Model Reconstruction Based on Pix4Dmapper - Spatial Analysis and Site Selection
- 地理探测器Geodetector软件的下载、应用与结果解读
- 直播课堂系统09--腾讯云点播管理模块(一)
猜你喜欢

Alibaba and Ant Group launched OceanBase 4.0, a distributed database, with single-machine deployment performance exceeding MySQL

翻译科技论文,俄译中怎样效果好

JVM classic fifty questions, now the interview is stable

PROCEDURE :存储过程结构——《mysql 从入门到内卷再到入土》

JVM经典五十问,这下面试稳了

Shell programming specification and variables

camera preview process --- from HAL to OEM

C. Even Picture

ACM模板笔记:最长不下降/上升子序列

LeetCode-36-Binary search tree and doubly linked list
随机推荐
[SQL brush questions] Day3----Special exercises for common functions that SQL must know
卡片盒笔记法的操作步骤
xshell (sed 命令)
shell编程之免交互
从斐波那契 - 谈及动态规划 - 优化
LeetCode-498 - Diagonal Traversal
力扣215题,数组中的第K个最大元素
F. Binary String Reconstruction
数字化转型:如何引导创新领导者
国内Gravatar头像的完美替代方案Cravatar
labelme - block drag and drop events
Huawei router clock near the drainage experiment (using stream strategy)
LeetCode-498-对角线遍历
黑猫带你学Makefile第12篇:常见Makefile问题汇总
我的世界整合包 云服务器搭建方法(ECS)
内置模板市场,DataEase开源数据可视化分析平台v1.13.0发布
D. Game With Array
【SQL刷题】Day3----SQL必会的常用函数专项练习
ArcMap创建镶嵌数据集、导入栅格图像并修改像元数值显示范围
流程控制结构——《mysql 从入门到内卷再到入土》
