用pytest写接口自动化
时间: 2023-06-30 20:23:57 浏览: 69
好的,对于使用 pytest 编写接口自动化测试,可以按照以下步骤进行:
1. 安装 pytest 和 requests 库
在命令行中输入以下命令进行安装:
```
pip install pytest requests
```
2. 编写测试用例
在项目目录下创建一个 tests 文件夹,并在其中编写测试用例。例如,创建一个名为 test_example.py 的文件,其中包含以下代码:
```python
import requests
def test_example():
url = 'http://httpbin.org/get'
params = {'key': 'value'}
r = requests.get(url, params=params)
assert r.status_code == 200
```
这是一个简单的测试用例,使用 requests 库发送一个 GET 请求,并断言响应状态码为 200。
3. 运行测试用例
在命令行中进入项目目录,并输入以下命令运行测试:
```
pytest
```
pytest 会自动查找 tests 文件夹下的测试用例,并执行它们。如果测试用例运行成功,pytest 会输出测试结果。
以上就是使用 pytest 编写接口自动化测试的基本步骤。当然,还有很多其他的 pytest 功能可以使用,例如参数化测试、测试报告生成等,可以根据具体需求进行深入学习。
相关问题
使用pytest实现接口自动化案例
以下是一个使用pytest实现接口自动化案例的示例:
假设我们要测试一个简单的接口,它的地址为:http://www.example.com/api/get_user,请求方法为GET,参数为user_id,返回json格式的用户信息。
第一步:安装pytest和requests
在命令行中输入以下命令安装pytest和requests:
```
pip install pytest requests
```
第二步:创建测试文件
在项目根目录下创建一个名为test_api.py的文件,在文件中编写测试用例代码:
```python
import pytest
import requests
@pytest.mark.parametrize("user_id", [1, 2, 3])
def test_get_user(user_id):
url = "http://www.example.com/api/get_user"
params = {"user_id": user_id}
response = requests.get(url, params=params)
assert response.status_code == 200
assert response.json()["user_id"] == user_id
```
该测试文件中定义了一个测试函数test_get_user,使用了pytest提供的@parametrize装饰器来实现参数化测试,测试的参数为1、2、3三个不同的用户id。
测试函数中首先定义了接口地址和请求参数,然后使用requests库发送get请求,并对返回结果进行断言,判断接口是否返回了正确的状态码和用户信息。
第三步:运行测试用例
在命令行中进入项目根目录,执行以下命令运行测试:
```
pytest test_api.py
```
如果所有的测试用例都运行通过,命令行将输出“X passed”,表示测试用例全部通过。
上述示例仅是一个简单的接口测试案例,实际应用中可能需要更复杂的测试场景,需要根据实际情况编写测试用例代码。但是,使用pytest框架可以大大简化测试用例的编写和管理,提高测试效率和可维护性。
pytest登录接口自动化
pytest是一个流行的Python测试框架,它非常适合用于接口自动化测试,尤其是针对Web应用的API测试。登录接口的自动化测试通常包括验证用户输入的有效性、处理登录过程、检查登录后的状态等步骤。以下是如何使用pytest进行登录接口自动化的一般步骤:
1. 安装依赖:首先,确保已经安装了`requests`库,它是用来发送HTTP请求的,以及pytest本身。
```bash
pip install requests pytest
```
2. 定义测试模块:创建一个测试文件,如`test_login.py`,导入所需的pytest和requests模块。
3. 创建测试函数:为登录接口创建一个或多个测试函数,例如`test_valid_login`和`test_invalid_login`。
```python
def test_valid_login(client, auth_user):
response = client.post('/login', json=auth_user)
assert response.status_code == 200
# 验证登录后可能返回的token或session信息
def test_invalid_login(client):
invalid_credentials = {'username': 'invalid', 'password': 'wrong'}
response = client.post('/login', json=invalid_credentials)
assert response.status_code == 401
# 验证错误响应的状态码和可能的消息
```
`client`通常是一个`fixture`,可以是`pytest-httpx`提供的`Client`对象,用于模拟HTTP请求;`auth_user`可能是另一个fixture,包含有效的用户名和密码字典。
4. 使用fixtures设置环境:在`conftest.py`或其他适当的配置文件中,定义`client`和`auth_user`等fixture。
```python
import httpx
@pytest.fixture
async def client():
async with httpx.AsyncClient() as client:
yield client
@pytest.fixture
def auth_user():
return {'username': 'testuser', 'password': 'testpassword'}
```
5. 运行测试:在命令行中运行`pytest test_login.py`来执行登录接口的自动化测试。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)