当前位置:网站首页>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步可实现接口自动化测试
边栏推荐
- Shell编程之条件语句(二)
- JVM经典五十问,这下面试稳了
- B. Same Parity Summands
- JS中的filter、map、reduce
- UPDATE:修改数据语法使用例——《mysql 从入门到内卷再到入土》
- How to secure users in LDAP directory service?
- APP UI自动化测试常见面试题,或许有用呢~
- Detailed explanation of the use of Oracle's windowing function (2)
- shell小技巧(一百三十五)打包指定目录下所用文件,每个文件单独打包
- 玩转doxygen 之RT-THREAD
猜你喜欢
PROCEDURE :存储过程结构——《mysql 从入门到内卷再到入土》
直播课堂系统08补-腾讯云对象存储和课程分类管理
【PCBA方案设计】蓝牙跳绳方案
Use Cloudreve to build a private cloud disk
带你一文读懂SaaS版多租户商城系统对多品牌企业的应用价值
Live Classroom System 08-Tencent Cloud Object Storage and Course Classification Management
Rider调试ASP.NET Core时报thread not gc-safe的解决方法
Redis Performance Impact - Asynchronous Mechanisms and Response Latency
化学制品制造业数智化供应链管理系统:建立端到端供应链采购一体化平台
LeetCode-498-对角线遍历
随机推荐
内置模板市场,DataEase开源数据可视化分析平台v1.13.0发布
数据标注太昂贵?这个方法可以用有限的数据训练模型实现基于文本的ReID!
HighTec shortcut keys (Keys) setting location
2022.8.8 Selected Lectures on Good Topics (Number Theory Field)
2022.8.8好题选讲(数论场)
APP UI自动化测试常见面试题,或许有用呢~
labelme-屏蔽拖拽的事件
DDL:ALTER 修改数据库——《mysql 从入门到内卷再到入土》
自组织是管理者和成员的双向奔赴
Intelligent scheme design - intelligent rope skipping scheme
基于Pix4Dmapper的空间三维模型重建应用——空间分析选址
黑猫带你学Makefile第11篇:当头文件a.h改变时,如何将所有依赖头文件a.h的.c文件都重新编译
RTL8721DM 双频WIFI + 蓝牙5.0 物联网(IoT)应用
从斐波那契 - 谈及动态规划 - 优化
ACM模板笔记:八数码问题——使用BFS+康托展开打表解决
根心与根轴
直播课堂系统08-腾讯云对象存储和课程分类管理
wget编译升级故障解决
翻译科技论文,俄译中怎样效果好
CGO Preliminary Cognition and Basic Data Type Conversion