Pytest中conftest.py的用法
作者:RockChe''s Blog 时间:2022-08-05 03:48:32
前言
在之前介绍fixture的文章中,我们使用到了conftest.py文件,那么conftest.py文件到底该如何使用呢,下面我们就来详细了解一下conftest.py文件的特点和使用方法吧
什么是conftest.py
我们之前了解了fixture,fixture可以直接定义在测试脚本中,但是有些时候,我们希望一个fixture可以被复用,这就需要对fixture进行集中管理,Pytest使用文件conftest.py
集中管理固件.在复杂的项目中,可以在不同的目录层级定义conftest.py,其作用域为其所在的目录和子目录,通常情况下,conftest.py
和@pytest.fixture()
会结合使用,来实现全局的前后置处理。
conftest.py特点
conftest.py
文件的名称是固定的,不能修改conftest.py
与运行的用例要在同一个pakage下,并且有__init__.py
文件不需要
import
导入conftest.py
文件,pytest用例会自动识别该文件,放到根目录下可以全局目录调用,放在某个package下,那就在该package内有效不同目录可以有自己的conftest.py,一个项目中可以有多个
conftest.py
pytest会默认读取
conftest.py
里面的所有fixture,所有同目录测试文件运行前都会执行conftest.py
文件
conftest.py用法
在我们实际的测试中,conftest.py文件需要结合fixture来使用,所以fixture中参数scope也适用conftest.py中fixture的特性,这里再说明一下
conftest中fixture的scope参数为session,所有的测试文件执行前(后)执行一次
conftest.py
文件中的fixture。conftest中fixture的scope参数为module,每一个测试.py文件执行前(后)都会执行一次
conftest.py
文件中的fixtureconftest中fixture的scope参数为class,每一个测试文件中的测试类执行前(后)都会执行一次
conftest.py
文件中的fixtureconftest中fixture的scope参数为function,所有文件的测试用例执行前(后)都会执行一次
conftest.py
文件中的fixture
conftest.py实际案例
我们按照这样的目录新建一个项目
在根目录conftestdemo下
根目录中的conftest.py文件中,一般写全局的fixture,比如登录
conftest.py
import pytest
@pytest.fixture(scope="session")
def login():
print("***登录成功,返回用户名***")
name = "rockche"
yield name
print("***退出登录***")
@pytest.fixture(autouse=True)
def get_name(login):
name = login
print(f"--每个用例都调用外层fixiture:打印用户name:{name}--")
根目录下的测试用例
test_1.py
def test_get_name(login):
name = login
print("***基础用例:获取用户name***")
print(f"用户名:{name}")
运行conftestdemo下的所有用例
run.py
import pytest
if __name__ == '__main__':
pytest.main(["-s", "../conftestdemo/"])
test_baidu目录下
配置针对baidu网站的测试用例独有的fixture
conftest.py
import pytest
@pytest.fixture(scope="module")
def open_baidu(login):
name = login
print(f"用户 {name} 打开baidu")
test_case1.py
def test_case2_01(open_baidu):
print("搜索pytest")
def test_case2_02(open_baidu):
print("搜索博客园")
test_cnblogs目录下
没有__init__.py
文件也没有conftest.py文件
test_case1.py
def test_no_fixture(login):
print("没有__init__文件,直接进入cnblogs", login)
test_taobao目录下
配置针对taobao网站的测试用例独有的fixture
conftest.py
import pytest
@pytest.fixture(scope="function")
def open_taobao(login):
name = login
print(f"用户 {name} 进入淘宝")
test_case1.py
class TestTaobao:
def test_case1_01(self, open_taobao):
print("选购商品")
def test_case1_02(self, open_taobao):
print("进入结算界面")
运行run.py
来源:https://www.cnblogs.com/crdym/p/14940291.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python命令行参数解析OptionParser类用法实例
![](https://img.aspxhome.com/file/2023/6/70736_0s.gif)
python通过pillow识别动态验证码的示例代码
![](https://img.aspxhome.com/file/2023/5/62295_0s.png)
PHP自定义函数格式化json数据示例
Vue.js结合SortableJS实现树形数据拖拽
![](https://img.aspxhome.com/file/2023/1/130131_0s.png)
PyQt5打开文件对话框QFileDialog实例代码
PHP中的session永不过期的解决思路及实现方法分享
解决pycharm19.3.3安装pyqt5找不到designer.exe和pyuic.exe的问题
![](https://img.aspxhome.com/file/2023/3/79793_0s.png)
完美的js验证网址url(正则表达式)
Python3爬虫中识别图形验证码的实例讲解
![](https://img.aspxhome.com/file/2023/3/90383_0s.png)
Python实现字符串中某个字母的替代功能
Python 连接字符串(join %)
Python json读写方式和字典相互转化
![](https://img.aspxhome.com/file/2023/0/85310_0s.png)
MySQL replace into 语句浅析(一)
![](https://img.aspxhome.com/file/2023/4/129894_0s.jpg)
Go语言开发框架反射机制及常见函数示例详解
![](https://img.aspxhome.com/file/2023/5/105135_0s.png)
通过Python中的CGI接口讲解什么是WSGI
ASP实例:处理多关键词查询实例代码
Python asyncio异步编程简单实现示例
![](https://img.aspxhome.com/file/2023/9/68419_0s.jpg)