pytest + ddt 使用方法

在尝试将ddt的数据驱动与pytest框架结合时遇到了问题,当使用`file_data`装饰器从yaml文件加载数据时,运行pytest命令会报找不到fixture 'value'的错误。已知ddt是基于unittest的,而代码使用的是pytest框架。目前解决方案似乎是将测试类集成到unittest中,但寻求将ddt与pytest无缝集成的方法。
摘要由CSDN通过智能技术生成

最近看到了ddt数据驱动很是牛逼,但是貌似不能和pytest集合,代码都是用pytest框架写的,悲剧了。

 

from ddt import ddt, data, file_data

@ddt
class TestFooTest():

    @file_data('test_create_file.yaml')
    def test_two(self, value):
            print(value)

 

执行的话就报错了,只能是类集成了unitest才能成功。或者谁有解决办法欢迎留言。

(venv) [17:39:57 root@master test]$ pytest ddt_test.py
======================================================== test session starts ===================================================

搭建一个基于Python + requests + pytest + ddt + unittest 的自动化测试框架,可以按照以下步骤进行: 1. 安装所需依赖:在你的项目环境中安装 requests、pytestddt 和 unittest。 ``` pip install requests pytest ddt unittest ``` 2. 创建项目结构:在你的项目目录下创建以下文件和文件夹结构: ``` ├── tests │ ├── __init__.py │ └── test_cases.py ├── data │ └── test_data.xlsx └── conftest.py ``` 3. 编写测试用例:在 `test_cases.py` 文件中编写你的测试用例,可以使用 `ddt` 来实现数据驱动,示例: ```python import unittest import ddt from data.test_data import TestData from utils.api_helper import APIClient @ddt.ddt class APITestCase(unittest.TestCase): @classmethod def setUpClass(cls): cls.client = APIClient() @ddt.data(*TestData.test_data) def test_api(self, data): response = self.client.send_request(data['url'], data['method'], data['payload']) self.assertEqual(response.status_code, data['expected_status']) self.assertEqual(response.json(), data['expected_response']) ``` 4. 创建测试数据:在 `test_data.xlsx` 文件中创建测试数据,可以使用 `openpyxl` 库来读取 Excel 数据,示例: ```python import openpyxl class TestData: workbook = openpyxl.load_workbook('data/test_data.xlsx') sheet = workbook['Sheet1'] test_data = [] for row in sheet.iter_rows(min_row=2, values_only=True): test_data.append({ 'url': row[0], 'method': row[1], 'payload': row[2], 'expected_status': row[3], 'expected_response': row[4] }) ``` 5. 创建 API 辅助类:在 `api_helper.py` 文件中创建一个 APIClient 类,用于发送 API 请求,示例: ```python import requests class APIClient: def send_request(self, url, method, payload): if method == 'GET': response = requests.get(url, params=payload) elif method == 'POST': response = requests.post(url, json=payload) elif method == 'PUT': response = requests.put(url, json=payload) elif method == 'DELETE': response = requests.delete(url) else: raise ValueError('Invalid HTTP method') return response ``` 6. 创建 pytest 配置:在 `conftest.py` 文件中配置 pytest,示例: ```python import pytest def pytest_addoption(parser): parser.addoption("--base-url", action="store", default="http://localhost:8000", help="Base URL for API tests") @pytest.fixture(scope="session") def base_url(request): return request.config.getoption("--base-url") ``` 7. 运行测试:在命令行中执行以下命令来运行测试用例: ``` pytest -s tests/ ``` `-s` 参数用于显示打印信息。 这样,你就搭建了一个基于 Python + requests + pytest + ddt + unittest 的自动化测试框架,可以进行接口测试。你可以根据实际需求进一步扩展和优化这个框架。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值