python接口自动化+request库实现登录获取token
时间: 2024-02-03 10:04:25 浏览: 36
好的,下面是一个简单的python接口自动化示例,用于实现登录获取token。
```python
import requests
url = 'http://example.com/login'
data = {
'username': 'your_username',
'password': 'your_password'
}
response = requests.post(url, data=data)
if response.status_code == 200:
token = response.json().get('token')
print('token:', token)
else:
print('登录失败')
```
以上代码使用了requests库来发送POST请求,向指定的URL提交用户名和密码。如果登录成功,服务器会返回一个JSON格式的响应,其中包含了token。我们可以通过response.json().get('token')来获取并打印出来。
当然,这只是一个简单的示例,实际的接口自动化需要考虑更多的情况,比如登录失败的处理、非正常响应的处理等。
相关问题
python接口自动化+request库+pytest框架+PO模式封装实现登录获取token
好的,下面是一个简单的示例代码:
1. 安装依赖库
```
pip install requests
pip install pytest
```
2. 创建项目结构
```
.
├── api
│ ├── __init__.py
│ └── login.py
├── case
│ ├── __init__.py
│ └── test_login.py
├── common
│ ├── __init__.py
│ └── utils.py
└── pytest.ini
```
3. 编写代码
api/login.py
```python
import requests
class LoginApi:
def __init__(self):
self.url = 'http://localhost:8080/api/login'
def login(self, username, password):
payload = {
'username': username,
'password': password
}
response = requests.post(self.url, json=payload)
return response.json().get('data').get('token')
```
common/utils.py
```python
import pytest
from api.login import LoginApi
@pytest.fixture(scope='session')
def token():
api = LoginApi()
return api.login('test', '123456')
```
case/test_login.py
```python
def test_login(token):
assert token is not None
```
4. 运行测试
在项目根目录下运行以下命令:
```
pytest
```
输出结果:
```
============================= test session starts =============================
platform darwin -- Python 3.8.5, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /Users/username/project
collected 1 item
case/test_login.py . [100%]
============================== 1 passed in 0.17s ==============================
```
这样就完成了一个简单的接口自动化测试,并且封装了登录获取 token 的 API 和使用了 pytest 的 fixture 机制。
搭建一个pytest+allure+request接口自动化框架
好的,这里为您提供一个简单的pytest+allure+requests接口自动化框架搭建步骤:
1. 安装pytest和allure
```
pip install pytest
pip install allure-pytest
```
2. 安装requests库
```
pip install requests
```
3. 创建测试用例文件和测试数据文件
在项目中创建一个tests文件夹,用于存放测试用例文件。在tests文件夹中创建一个test_api.py文件,用于编写测试用例。
同时,也需要在项目中创建一个data文件夹,用于存放测试数据文件。在data文件夹中创建一个api_data.json文件,用于存放接口测试数据。
4. 编写测试用例
在test_api.py文件中,使用pytest编写测试用例。可以使用requests库发送接口请求,并对返回结果进行断言。
示例代码:
```python
import pytest
import requests
class TestAPI:
@pytest.fixture(scope='session', autouse=True)
def setup_class(self):
self.base_url = 'https://api.github.com'
def test_get_user(self):
url = self.base_url + '/users/github'
response = requests.get(url)
assert response.status_code == 200
assert response.json()['login'] == 'github'
def test_create_gist(self):
url = self.base_url + '/gists'
data = {
"description": "test gist",
"public": True,
"files": {
"test.txt": {
"content": "Test gist content"
}
}
}
headers = {
"Authorization": "token <your access token>"
}
response = requests.post(url, json=data, headers=headers)
assert response.status_code == 201
```
5. 编写测试数据
在api_data.json文件中,编写接口测试用到的数据。可以对不同接口的测试数据进行分类,方便维护。
示例代码:
```json
{
"get_user": {
"url": "/users/github",
"method": "get",
"headers": {},
"params": {},
"data": {},
"json": {},
"expected_status_code": 200,
"expected_data": {
"login": "github"
}
},
"create_gist": {
"url": "/gists",
"method": "post",
"headers": {
"Authorization": "token <your access token>"
},
"params": {},
"data": {
"description": "test gist",
"public": true,
"files": {
"test.txt": {
"content": "Test gist content"
}
}
},
"json": {},
"expected_status_code": 201,
"expected_data": {}
}
}
```
6. 使用pytest执行测试用例
在项目根目录下打开终端,执行如下命令:
```
pytest --alluredir=./report
```
该命令将会执行tests文件夹下的所有测试用例,并将测试结果保存在report文件夹中。
7. 生成测试报告
执行完测试用例后,使用如下命令生成测试报告:
```
allure generate ./report -o ./report/html --clean
```
该命令将会在report文件夹下生成一个html文件夹,里面包含测试结果的详细信息和图表。
至此,一个简单的pytest+allure+requests接口自动化框架就搭建完成了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)