用python 写一个接口测试
时间: 2024-05-29 07:03:55 浏览: 14
好的,我来给你写一个简单的 Python 接口测试例子。我们将使用 requests 库来发送 HTTP 请求。
首先,你需要安装 requests 库:
```python
pip install requests
```
然后,你需要准备一个接口地址和参数。例如,我们来测试一个天气预报接口:
```python
import requests
url = "https://api.openweathermap.org/data/2.5/weather"
params = {
"q": "London,UK",
"appid": "your_api_key"
}
```
接下来,我们可以使用 requests 库发送 GET 请求:
```python
response = requests.get(url, params=params)
print(response.status_code)
print(response.text)
```
这里,我们打印出了响应状态码和响应内容。你可以根据实际情况进行断言和验证。
完整的代码如下:
```python
import requests
url = "https://api.openweathermap.org/data/2.5/weather"
params = {
"q": "London,UK",
"appid": "your_api_key"
}
response = requests.get(url, params=params)
print(response.status_code)
print(response.text)
```
注意,这里的 `your_api_key` 需要替换成你自己的 API 密钥。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)