PyTest 的基本用法

1.pytest安装


1.1安装

pip install -U pytest


1.2验证安装

pytest --version # 会展示当前已安装版本


1.3pytest文档


官方文档:
https://docs.pytest.org/en/latest/contents.html
在pytest框架中,有如下约束:
所有的单测文件名都需要满足test_*.py格式或*_test.py格式。

在单测文件中,测试类以Test开头,并且不能带有 init 方法(注意:定义class时,需要以T开头,不然pytest是不会去运行该class的)
在单测类中,可以包含一个或多个test_开头的函数。
此时,在执行pytest命令时,会自动从当前目录及子目录中寻找符合上述约束的测试函数来执行。


1.4 Pytest运行方式

# file_name: test_abc.py 
import pytest # 引入pytest包 
def test_a(): # test开头的测试函数 
print("------->test_a") 
assert 1 # 断言成功 
def test_b(): 
print("------->test_b") 
assert 0 # 断言失败 
if __name__ == '__main__': 
pytest.main("-s test_abc.py") # 调用pytest的main函数执行测试


1.测试类主函数模式

pytest.main("-s test_abc.py")


2.命令行模式


pytest 文件路径/测试文件名
例如:pytest ./test_abc.py

1.5 Pytest Exit Code 含义清单

  • Exit code 0 所有用例执行完毕,全部通过
  • Exit code 1 所有用例执行完毕,存在Failed的测试用例
  • Exit code 2 用户中断了测试的执行
  • Exit code 3 测试执行过程发生了内部错误
  • Exit code 4 pytest 命令行使用错误
  • Exit code 5 未采集到可用测试用例文件


1.6 如何获取帮助信息
查看 pytest 版本

pytest --version


显示可用的内置函数参数

pytest --fixtures


通过命令行查看帮助信息及配置文件选项
pytest --help

1.7 控制测试用例执行
1.在第N个用例失败后,结束测试执行

pytest -x # 第01次失败,就停止测试
pytest --maxfail=2 # 出现2个失败就终止测试


2.指定测试模块


pytest test_mod.py


3.指定测试目录
pytest testing/

扫描二维码关注公众号,回复: 16842131 查看本文章


4.通过关键字表达式过滤执行
pytest -k "MyClass and not method"
这条命令会匹配文件名、类名、方法名匹配表达式的用例,这里这条命令会运行
TestMyClass.test_something, 不会执行 TestMyClass.test_method_simple


5.通过 node id 指定测试用例
nodeid由模块文件名、分隔符、类名、方法名、参数构成,举例如下:
运行模块中的指定用例
pytest test_mod.py::test_func
运行模块中的指定方法
ytest test_mod.py::TestClass::test_method


6.通过标记表达式执行
pytest -m slow
这条命令会执行被装饰器 @pytest.mark.slow 装饰的所有测试用例


7.通过包执行测试
pytest --pyargs pkg.testing
这条命令会自动导入包 pkg.testing,并使用该包所在的目录,执行下面的用例。

1.8 多进程运行cases
当cases量很多时,运行时间也会变的很长,如果想缩短脚本运行的时长,就可以用多进程来运行。
安装pytest-xdist:
pip install -U pytest-xdist
运行模式:
pytest test_se.py -n NUM
其中NUM填写并发的进程数。

1.9 重试运行cases
在做接口测试时,有事会遇到503或短时的网络波动,导致case运行失败,而这并非是我们期望的结果,此时可以就可以通过重试运行cases的方式来解决。
安装pytest-rerunfailures:
pip install -U pytest-rerunfailures
运行模式:
pytest test_se.py --reruns NUM
NUM填写重试的次数。

1.10 显示print内容
在运行测试脚本时,为了调试或打印一些内容,我们会在代码中加一些print内容,但是在运行pytest时,这些内容不会显示出来。如果带上-s,就可以显示了。
运行模式:
pytest test_se.py -s
另外,pytest的多种运行模式是可以叠加执行的,比如说,你想同时运行4个进程,又想打印出print的内容。可以用:
pytest test_se.py -s -n 4

2.Pytest的setup和teardown函数
1.setup和teardown主要分为:模块级,类级,功能级,函数级。
2.存在于测试类内部
代码示例:

  • 函数级别setup()/teardown()

运行于测试方法的始末,即:运行一次测试函数会运行一次setup和teardown

import pytest 
class Test_ABC: # 函数级开始 
def setup(self): 
print("------->setup_method") # 函数级结束 
def teardown(self): 
print("------->teardown_method") 
def test_a(self): 
print("------->test_a") assert 1 
def test_b(self): 
print("------->test_b") 
if __name__ == '__main__':
pytest.main("-s test_abc.py")


执行结果:

test_abc.py
------->setup_method # 第一次 setup()
------->test_a
.
------->teardown_method # 第一次 teardown()
------->setup_method # 第二次 setup()
------->test_b
.
------->teardown_method # 第二次 teardown()


2.2.类级别


运行于测试类的始末,即:在一个测试内只运行一次setup_class和teardown_class,不关心测试类内有多少个测试函数。
代码示例:

import pytest class Test_ABC: # 测试类级开始 
def setup_class(self):
print("------->setup_class") # 测试类级结束 
def teardown_class(self): 
print("------->teardown_class") 
def test_a(self): 
print("------->test_a") assert 1 
def test_b(self): 
print("------->test_b") 
if __name__ == '__main__': 
pytest.main("-s test_abc.py")


执行结果:


test_abc.py
------->setup_class # 第一次 setup_class()
------->test_a .
------->test_b F
------->teardown_class # 第一次 teardown_class()

3.Pytest配置文件

pytest的配置文件通常放在测试目录下,名称为pytest.ini,命令行运行时会使用该配置文件中的配置.
#配置pytest命令行运行参数 [pytest] addopts = -s ... # 空格分隔,可添加多个命令行参数 -所有参数均为插件包的参数配置测试搜索的路径 testpaths = ./scripts # 当前目录下的scripts文件夹 -可自定义 #配置测试搜索的文件名称 python_files = test*.py #当前目录下的scripts文件夹下,以test开头,以.py结尾的所有文件 -可自定义 配置测试搜索的测试类名 python_classes = Test_* #当前目录下的scripts文件夹下,以test开头,以.py结尾的所有文件中,以Test开头的类 -可自定义 配置测试搜索的测试函数名 python_functions = test_* #当前目录下的scripts文件夹下,以test开头,以.py结尾的所有文件中,以Test开头的类内,以test_开头的方法 -可自定义

4 Pytest常用插件


插件列表网址:
https://plugincompat.herokuapp.com
包含很多插件包,大家可依据工作的需求选择使用。
4.1 前置条件:
1.文件路径:
- Test_App - - test_abc.py - - pytest.ini
2.pyetst.ini配置文件内容:
[pytest] # 命令行参数 addopts = -s # 搜索文件名 python_files = test_*.py # 搜索的类名 python_classes = Test_* #搜索的函数名 python_functions = test_*


4.2 Pytest测试报告
pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告。兼容Python 2.7,3.6
安装方式:pip install pytest-html
pip install pytest-html
通过命令行方式,生成xml/html格式的测试报告,存储于用户指定路径。插件名称:pytest-html
使用方法: 命令行格式:pytest --html=用户路径/report.html
示例:

import pytest 
class Test_ABC: 
def setup_class(self): 
print("------->setup_class") 
def teardown_class(self): 
print("------->teardown_class") 
def test_a(self): 
print("------->test_a") assert 1 
def test_b(self): 
print("------->test_b") assert 0 # 断言失败``` 
运行方式: 1.修改Test_App/pytest.ini文件,添加报告参数,即:addopts = -s --html=./report.html 
# -s:输出程序运行信息 
# --html=./report.html 在当前目录下生成report.html文件 ️
 若要生成xml文件,可将--html=./report.html 改成 --html=./report.xml 
2.命令行进入Test_App目录 
3.执行命令: pytest 执行结果: 1.在当前目录会生成assets文件夹和report.html文件

猜你喜欢

转载自blog.csdn.net/x1987200567/article/details/131599283
今日推荐