当前位置:网站首页>pytest-fixture、conftest、mark的介绍和使用
pytest-fixture、conftest、mark的介绍和使用
2022-06-21 23:26:00 【不要问我y】
前言
在b站上找的学习视频,标题是pytest框架学习,跟着学了一段时间,写测试用例的时候发现为啥类的后面括号里写的是unittest.Testcase,学到现在我了解到pytest是unittest的升级版,具体有哪些区别等我后续学习了再来补充
1、pytest框架下用fixture,unittest下用setup、teardown(这个我也不确定对不对)
2、fixture介绍
fixture定义了构成测试准备阶段的步骤和数据
2.1 fixture 固定写法:
@pytest.fixture(scope = 'function',autouse = ‘True')
def my_fixture():
print("a")
或者
@pytest.fixture(scope = 'function',autouse = ‘True')
def my_fixture(request):
print('前置’)
#补充,yield起到一个分界线的作用,分开前后置
yiled request.pram //yield 和return都是返回的意思,但是yield后面可以继续写代码,return后面不可以
print(‘后置’)
2.2 fixture标记的函数,不能跟测试函数、测试类放在一起,我之前把fixture标记的方法写为test_开头,这是不对的
2.3 fixture的使用
- 被标记的函数作为参数传入测试用例中
@pytest.fixture()
def my_fixture():
print ("a")
def test_01(my_fixture):
print("使用fixture的一种方式“)
- 使用装饰器:
@pytest.mark.usefixtures(" ")
3、conftest
- 放的都是fixture
- fixture是可以对外共享-所有用例共享
- 优先级:就近原则
- 可以层级创建
- 不能跟测试用例放一块(不能以test_)命名被标记的函数
4、mark:标记,需要在pytest.ini文件中说明标记
pytest.ini中注明标记,-m表示只运行被标记了的测试用例
main中写:pytest.main(["-m smoke","-v"](可以放多个标签)
打标记范围:测试用例,测试类,模块
class TestCase(object):
#该类下的用例都会有这个标签
pytestmark = pytest.mark.xxx
#多标签模式
pytestmark = [pytest.mark.标签1,pytest.mark.标签2]
#该模块下的用例都会有这个标签
import pytest
pytestmark = pytest.mark.xxx
#多标签模式
pytestmark = [pytest.mark.标签1,pytest.mark.标签2]
5、失败重运行机制
安装插件:pip install pytest-rerunfailures
使用方式:pytest --returns 2 表示:运行失败的用例可以运行两次
延时:pytest --reruns 2 --reruns-delay x(秒)
6、main中的参数
pytest.main(["-s","-v", #显示详细的用例信息
"-m","标记1”, #运行标记了标记1的用例
”-m 标记1 and 标记2“ # 运行有标记1并且有标记2的用例
“--html= report.html", #输出html测试报告
"--alluredir = allure_dir", #输出allure测试报告
"--renruns","2","--reruns-delay","5"]) #标记了标记1的失败用例间隔5秒重跑2次
边栏推荐
- Why is the sample variance divided by (n-1)
- 【设置静态路由】“内网专用外网用WIFi“
- Getting started with go web programming: validators
- 在terminal拷贝粘贴,多了0~与1~字符
- [typescript] the difference between exclamation mark and question mark in typescript
- [node] node uses MySQL connection pool
- 【next】nextjs打包出现组件定义缺少显示名称【Component definition is missing display name】
- AttributeError: ‘WebDriver‘ object has no attribute ‘w3c‘
- Introduction to some code static checking tools
- Detailed explanation of IDA static reverse analysis tool
猜你喜欢
随机推荐
rabbit:do_run_postlaunch_phase/0 line 932
What do technical people pursue?
buuctf pwn ciscn_ 2019_ n_ eight
buuctf misc 弱口令
TCP/IP--路由选择
【php】mvcs概念(通俗易懂)
以父之名活动攻略(可以薅羊毛啦)
Hotline salon issue 26 - cloud security session
Root detection implementation
The tangled truth about NFT and copyright
如何优雅的统计代码耗时
[wechat applet] wechat applet uses pop-up box for multi-level linkage (example)
[Yugong series] general responsibility allocation principle in June 2022 (IX) - principle of protected variables
[GXYCTF2019]SXMgdGhpcyBiYXNlPw==
HarmonyOS应用开发第二次作业笔记
ARM32指令解析通用寄存器
JSONObject获取Date类型(getSqlDate)报错
[typescript] the difference between exclamation mark and question mark in typescript
Copy and paste in terminal, 0~ and 1~ characters are added
Based on asp Net development of enterprise communication source text message management platform source code







![[actf freshman competition 2020]swp](/img/80/9fe85ee614857c5800c0d0b1ba9a3d.png)

