当前位置:网站首页>pytest 筛选用例
pytest 筛选用例
2022-08-09 13:06:00 【沉觞流年】
pytest 筛选用例
指定目录查找用例
此时,项目目录是 class_pytest
,里面有两个测试用例的文件,test_demo1
和 test_demo2
,
test_demo1.py
def func(x):
return x
def test_answer1():
assert func(2)==2
class TestDemo:
def test_answer2(self):
assert func(5) == 5
def test_answer3(self):
assert func(4) == 5
test_demo2.py
def func2(x):
return x+1
def test_answer4():
assert func2(2)==3
打开终端,输入命令
pytest -s -v
# -s -v 这里是让控制台输出的内容更加详细
pytest 会默认查找当前目录下所有以test开头的函数或类里的函数,识别为用例
如果仅识别 demo 包下的用例
则可以在命令里加上相对路径名称,识别路径下的目录
pytest -s -v demo
# demo 是当前目录下的子目录
可以发现,只识别了 test_demo2.py
文件下的测试用例
pytest - mark
对测试用例打标签。在运行测试用例的时候,可根据标签名来过滤要运行的用例。
使用方法:
- 注册标签名
- 在测试用例/测试类前面加上: @pytest.mark.标记名
注册方式:
创建 pytest.ini
文件(必须如此命名,会自动识别),在文件中以如下形式添加标签名
[pytest]
markers =
demo : just for display
smoke
ini
文件中,section名称
和 option名称
的内容不能改变,标签名可以自定义,冒号后的为备注内容,如果无需备注,需连同冒号一起省略
pytest 可在函数、类上打标签;也可在一个用例上打多个标签,多次使用 @pytest.mark.标签名 即可。
示例:
test_demo1.py
import pytest
def func(x):
return x
def func2(x):
return x+1
@pytest.mark.demo
def test_answer():
assert func(5)==5
@pytest.mark.demo
@pytest.mark.smoke
class TestDemo:
def test_answer2(self):
assert func(4) == 5
def test_answer3(self):
assert func2(4) == 5
class TestDemo2:
def test_answer4(self):
assert func(4) == 5
@pytest.mark.demo
def test_answer5(self):
assert func2(4) == 5
在终端执行命令:
pytest -m demo -s -v
拓展:除了使用 @ 符号 打标记,还可以通过这种方式对 类 或 整个py文件下的所有用例 进行标记
import pytest
# 单标签模式
pytestmark = pytest.mark.demo
def func(x):
return x
def func2(x):
return x+1
def test_answer():
assert func(5)==5
class TestDemo:
# 多标签模式
pytestmark = [pytest.mark.demo,pytest.mark.smoke]
def test_answer2(self):
assert func(4) == 5
def test_answer3(self):
assert func2(4) == 5
class TestDemo2:
def test_answer4(self):
assert func(4) == 5
def test_answer5(self):
assert func2(4) == 5
与使用 使用 @ 符号打标记效果是一样的
运行过滤
除了在终端使用 pytest 命令运行过滤用例,还可通过建立 py文件的方式运行过滤用例
import pytest
pytest.main()
这个效果和在在终端输入 pytest 命令一样
若需要添加参数,筛选打上 demo
标签的用例,则采用以下方式
import pytest
pytest.main(["-s","-v","-m","demo"])
边栏推荐
猜你喜欢
GIN中GET POST PUT DELETE请求
kustomize entry example and basic syntax instructions
jenkins api create custom pipeline
现在40系显卡都快出来了,为何1060型号的显卡还有这么多人用?
微服务+微信小程序实现社区服务
音频基础学习——声音的本质、术语与特性
Jenkins API groovy calling practice: Jenkins Core Api & Job DSL to create a project
七夕力扣刷不停,343. 整数拆分(剑指 Offer 14- I. 剪绳子、剑指 Offer 14- II. 剪绳子 II)
蓝桥历届真题-跑步锻炼
Periodic sharing of Alibaba Da Tao system model governance
随机推荐
FFmpeg multimedia file processing (implementation of ffmpeg operation directory and list)
利用信号灯和共享内存实现进程间同步通信
X264性能优化
Process/Thread Related in Sandbox - 2
NC7 买卖股票的最好时机(一)
蓝桥历届真题-蛇形填数
陈强教授《机器学习及R应用》课程 第十七章作业
蓝桥历届真题-既约分数
gin's middleware and routing grouping
用plot_hist_numeric()实现画直方图
剑指 Offer 56 - II. 数组中数字出现的次数 II(位运算)
陈强教授《机器学习及R应用》课程 第十五章作业
程序员的七夕怎么过?不会是写代码吧
Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation
Q_04_05 使用Qubits
面试攻略系列(四)-- 你不知道的大厂面试
快来扔鸡蛋。
NC84 完全二叉树结点数
Sandbox中的进程/线程相关-2
GET POST PUT DELETE request in GIN