在软件开发中,测试是非常重要的一环。而测试框架的选择也是至关重要的。pytest 是一个非常流行的 Python 测试框架,它具有简单易用、灵活、可扩展等特点,可以帮助开发者更加高效地进行测试。
在测试过程中,经常需要读取一些配置文件,例如数据库连接信息、测试数据等。pytest 提供了一种方便的方式来读取和使用配置文件,可以帮助开发者更加方便地进行测试。
逻辑
pytest 读取和使用配置文件的逻辑如下:
1. 定义配置文件
首先需要定义一个配置文件,可以是一个 INI 文件、JSON 文件、YAML 文件等。配置文件中可以包含多个配置项,每个配置项包含一个键和一个值。例如,下面是一个示例 pytest.ini 文件:
```
[database]host = localhostport = 3306username = rootpassword = 123456database = test[general]run_test = Truerun_bsm = 450109102
```
2. 读取配置文件
pytest 提供了一个 `pytest_configure` 函数,可以在该函数中读取配置文件。该函数会在 pytest 启动时被调用,可以在该函数中进行一些初始化操作,例如读取配置文件、连接数据库等。例如,下面是一个示例代码:
```python
import configparserdef pytest_configure(config):# 读取配置文件config_file =config.ini # 文件路径conf = configparser.ConfigParser()conf.read(config_file)# 获取配置项host = conf.get('database', 'host')port = conf.getint('database','port')username = conf.get('database','username')password = conf.get('database','password')database = conf.get('database','database') # 连接数据库
# ...
```
在上面的代码中,首先定义了一个 `config_file` 变量,用于指定配置文件的路径。然后使用 `configparser` 模块读取配置文件,并获取了 `database` 配置项的值。最后可以根据配置项的值进行一些初始化操作,例如连接数据库。
3. 使用配置项
在测试代码中,可以使用 `pytest.fixture` 装饰器定义一个 fixture,用于获取配置项的值。例如,下面是一个示例代码:
```python
import pytest@pytest.fixturedef db_config(request):# 获取配置项host = request.config.getoption('db-host')port = request.config.getoption('db-port')username = request.config.getoption('db-username')password = request.config.getoption('db-password')database = request.config.getoption('db-database')return {host: host,port: port,username: username,password: password,database: database}def test_database_connection(db_config):# 使用配置项
# ...
```
在上面的代码中,首先使用 `pytest.fixture` 装饰器定义了一个 `db_config` fixture,用于获取配置项的值。在 fixture 中,使用 `request.config.getoption` 方法获取了命令行参数中指定的配置项的值,并返回一个字典。在测试代码中,可以使用 `db_config` fixture 获取配置项的值,并进行测试。
4. 运行测试
最后,在运行测试时,可以通过命令行参数指定配置文件的路径和配置项的值。例如,下面是一个示例命令:
```
pytest --config-file=config.ini --db-host=localhost --db-port=3306 --db-username=root --db-password=123456 --db-database=test
```
在上面的命令中,使用 `--config-file` 参数指定了配置文件的路径,使用 `--db-host`、`--db-port`、`--db-username`、`--db-password`、`--db-database` 参数指定了配置项的值。
代码示例
下面是一个完整的示例代码,用于演示 pytest 如何读取和使用配置文件:
```python
import configparserimport pytestdef pytest_configure(config):# 读取配置文件config_file =config.ini # 文件路径conf = configparser.ConfigParser()conf.read(config_file)# 获取配置项host = conf.get('database', 'host')port = conf.getint('database','port')username = conf.get('database','username')password = conf.get('database','password')database = conf.get('database','database') # 连接数据库@pytest.fixturedef db_config(request):# 获取配置项host = request.config.getoption('db-host')port = request.config.getoption('db-port')username = request.config.getoption('db-username')password = request.config.getoption('db-password')database = request.config.getoption('db-database')return {host: host,port: port,username: username,password: password,database: database}def test_database_connection(db_config):# 使用配置项print(fConnecting to database {db_config["database"]} at {db_config["host"]}:{db_config["port"]} # ...
```
在上面的代码中,首先在 `pytest_configure` 函数中读取了配置文件,并连接了数据库。然后在 `db_config` fixture 中获取了配置项的值,并返回一个字典。最后在 `test_database_connection` 函数中使用了 `db_config` fixture,并打印了连接数据库的信息。
在运行测试时,可以使用以下命令:
```
pytest --config-file=config.ini --db-host=localhost --db-port=3306 --db-username=root --db-password=123456 --db-database=test
```
运行结果如下:
```
============================= test session starts ==============================
platform win32 -- Python 3.8.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: D:\Projects\pytest-demo
collected 1 item
test_demo.py Connecting to database test at localhost:3306...
.
============================== 1 passed in 0.01s ===============================
```
可以看到,pytest 成功读取了配置文件,并连接了数据库。测试也成功通过了。
总结
pytest 提供了一种方便的方式来读取和使用配置文件,可以帮助开发者更加方便地进行测试。在测试代码中,可以使用 fixture 来获取配置项的值,并进行测试。在运行测试时,可以通过命令行参数指定配置文件的路径和配置项的值。
另外,作者给出一些:
configparser模块-读取配置文件的模块---仅供参考
ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容
创建的文件格式是cfg
文件内的格式:
[DEFAULT] # 全局的 [alex] # 用户名 Password = 123 # 密码 Quotation = 100 # 配额按MB计算 [jack] Password = 456 Quotation = 100
二、ConfigParser 初始化对象
import configparser # 导入模块
config = configparser.ConfigParser() # 实例化对象
config.read("ini", encoding="utf-8") # 对象读取文件
三、生成文件 # 最好用程序去生成,如果手动生成则会出现编码报错
import configparser
from server.conf import settings
conf = configparser.ConfigParser()
conf.read(settings.ACCOUNT_File)
conf.add_section("alex") # 新建一个属性
conf.set("alex", "Password", "123") # 对指定的属性下生成值
conf.write(open(settings.ACCOUNT_File,'w')) # 这里写入
config.sections() # 获取所需的section节点
config.options("db") # 获取指定section 的options
config.get("db", "db_host") # 获取指点section下指点option的值
config.getint("db", "k1") # 将获取到值转换为int型
config.getboolean("db", "k2" ) # 将获取到值转换为bool型
config.getfloat("db", "k3" ) # 将获取到值转换为浮点型
config.items("db") # 获取指点section的所用配置信息
config.set("db", "db_port", "69") # 修改某个option的值,如果不存在则会创建
config.has_section("section") # 检查是否存在该section,返回结果是bool值
config.has_option("section", "option") # 是检查section中否存在该option,返回结果是bool值
config.add_section("default") # 添加section
config.set("default", "db_host", "1.1.1.1") # 向section中添加option
config.remove_section("default") # 整个section下的所有内容都将删除
config.write(open("ini", "w")) # 增加删除新建都需要这行回写文件
最后: 可以在我的VX公众号:【自动化测试老司机】免费领取一份216页软件测试工程师面试宝典文档资料。以及相对应的视频学习教程免费分享!,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等。
Pytest是一个流行的Python测试框架,其提供了方便的方式来处理配置文件。开发者可以通过定义配置文件如INI,然后在pytest_configure函数中读取,如连接数据库。使用fixture,可以在测试代码中便捷地访问这些配置项,通过命令行参数指定配置。ConfigParser模块用于读取配置文件,支持多种操作如获取section和option的值。
1231

被折叠的 条评论
为什么被折叠?



