Python+request+pytest 接口自动化测试框架入门(与unittest的比较)

文章对比了pytest和unittest两个Python测试框架,指出pytest在参数化、测试报告详细度和断言简洁性上的优势,如pytest的@pytest.mark.parametrize装饰器实现参数化,以及pytest只需assert进行断言。此外,还提到了pytest的第三方插件支持,如pytest-html和allure报告。
摘要由CSDN通过智能技术生成

1. Python+request+pytest 接口自动化测试框架入门 - 简书

pytest和unittest的比较:

pytest是一个非常成熟的全功能的Python测试框架,主要有以下几个特点:

  1. 简单灵活,容易上手
  2. 支持参数化
  3. 能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests)
  4. pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等
  5. 测试用例的skip和xfail处理
  6. 可以很好的和jenkins集成
  7. report框架----allure 也支持了pytest

1. pytest更容易参数化,适用于更复杂的场景

1.unittest参数化

可以通过nose_parameterized来实现,格式:@nose_parameterized.parameterized.expand(data), ‘data’为list格式的参数化的数据

2.pytest参数化

通过装饰器@pytest.mark.parametrize来实现
 

 2.pytest的测试报告更详细,allure报告很详细

1.unittest

通过HTMLTestRunner生成

2.pytest

(1)通过pytest-html生成html格式报告

(2)通过allure生成方案(很详细)

 3.pytest断言更简洁

1.unittest 断言

assertEqual(a, b) # 判断a和b是否相等

assertNotEqual(a, b) # 判断a不等于b

assertTrue(a) # 判断a是否为Ture

assertFalse(a) #判断a是否为False

assertIn(a, b) # a 包含在b里面

asserNotIn(a, b) # a 不包含在b里面

……

2.pytest 断言

pytest只需要用assert 来断言就行,assert 后面加需要断言的条件就可以了,例如:assert a = = b # 判断a是否等于b、

assert a != b # 判断a不等于b、assert a in b # 判断b包含a

总结:从断言上面来看,pytest的断言比unittest要简单些,unittest断言需要记很多断言格式,pytest只有assert一个表达式,用起来比较方便
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
搭建一个基于Python + requests + pytest + ddt + unittest 的自动化测试框架,可以按照以下步骤进行: 1. 安装所需依赖:在你的项目环境中安装 requests、pytest、ddt 和 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 的自动化测试框架,可以进行接口测试。你可以根据实际需求进一步扩展和优化这个框架
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值