Python+Requests零基础系统掌握接口自动化【Z新无密】

268 阅读4分钟

Python+Requests零基础系统掌握接口自动化

核心代码,注释必读

// download:3w ukoou com

基于Python语言和pytest框架。

  1. 安装pytest

首先需要安装pytest,可以通过pip命令进行安装:

Copy code
pip install pytest
  1. 编写测试用例

在编写测试用例之前,需要确定接口的请求方式、请求地址、请求参数和预期结果等信息。在本例中,我们假设要测试的接口为一个简单的加法接口,其请求方式为POST,

测试用例代码如下:

pythonCopy code
import requests
import json
import pytest

@pytest.mark.parametrize("a, b, expected", [(1, 2, 3), (10, 20, 30), (-1, 1, 0)])
def test_addition(a, b, expected):
    url = "http://example.com/addition"
    data = {"a": a, "b": b}
    headers = {"Content-Type": "application/json"}
    response = requests.post(url, data=json.dumps(data), headers=headers)
    assert response.status_code == 200
    assert response.json()["result"] == expected

这里使用了pytest框架的参数化功能,将三组测试数据分别传入测试用例中,从而可以对不同的输入值进行测试。

  1. Python+Requests零基础系统掌握接口自动化运行测试用例

在命令行中进入测试用例所在目录,执行以下命令来运行测试用例:

Copy code
pytest

这将会自动搜索当前目录下以“test_*.py”命名的文件,并运行其中的所有测试用例。在本例中,我们将测试用例保存为“test_addition.py”。

  1. 查看测试结果

pytest会自动输出测试结果,包括每个测试用例的执行情况、总共运行的测试用例数量和通过的测试用例数量等信息。

如果有测试用例未通过,pytest会显示失败原因和具体的错误信息。

上述就是一个简单的接口自动化测试框架使用教程和代码实现。当然,实际的测试场景可能更加复杂,需要考虑各种异常情况和边界条件,但基本的流程和思路是类似的。

Python+Requests零基础系统掌握接口自动化综合实战

它使用Python编写,使用requests和unittest库来执行测试。

测试的目标是一个公共API,该API允许用户查询有关特定城市天气情况的信息。我们将测试以下功能:

  1. 验证API是否能够正确响应请求。
  2. 验证API返回的数据是否符合预期格式。
  3. 验证API返回的数据是否与实际天气情况相符。

我们将测试以下URL:api.openweathermap.org/data/2.5/we…

首先,我们需要准备测试文件,并导入必要的库和类:

pythonCopy code
import requests
import unittest

class APITestCase(unittest.TestCase):
    def test_response_code(self):
        pass
    
    def test_response_format(self):
        pass
    
    def test_weather_data(self):
        pass

现在,我们可以开始编写测试用例。第一个测试用例将验证API是否能够正确响应请求:

pythonCopy code
def test_response_code(self):
    city = "New York"
    appid = "your_api_key_here"
    
    url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={appid}"
    response = requests.get(url)
    
    self.assertEqual(response.status_code, 200)

这个测试用例向API发送一个GET请求,然后断言响应的状态码是否为200。如果状态码不是200,测试将失败。

第二个测试用例将验证API返回的数据是否符合预期格式:

pythonCopy code
def test_response_format(self):
    city = "New York"
    appid = "your_api_key_here"
    
    url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={appid}"
    response = requests.get(url)
    data = response.json()
    
    self.assertTrue("coord" in data)
    self.assertTrue("weather" in data)
    self.assertTrue("main" in data)
    self.assertTrue("wind" in data)
    self.assertTrue("clouds" in data)
    self.assertTrue("sys" in data)
    self.assertTrue("name" in data)

这个测试用例向API发送一个GET请求,然后将响应数据解析为JSON格式。然后,它断言JSON对象是否包含特定的键,这些键是我们期望在响应中看到的键。如果任何一个键不存在,测试将失败。

最后一个测试用例将验证API返回的数据是否与实际天气情况相符:

pythonCopy code
def test_weather_data(self):
    city = "New York"
    appid = "your_api_key_here"
    
    url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={appid}"
    response = requests.get(url)
    data = response.json()
    
    temperature = data["main"]["temp"]
    weather_conditions = [weather["main"] for weather in data["weather"]]
    
    self.assertTrue(temperature > 0 and temperature < 50)
    self.assertIn("Clouds", weather_conditions)

这个测试用例向API发送一个GET请求,然后将响应数据解析为JSON格式。然后,它提取API返回的温度和天气状况,并使用断言验证这些值是否符合预期。在这种情况下,我们验证温度是否在0到50摄氏度之间,并且天气状况是否为"Clouds"。

现在,我们可以使用unittest库的TestRunner运行这些测试用例:

pythonCopy code
if __name__ == '__main__':
    unittest.main()

Python+Requests零基础系统掌握接口自动化