接口mock与自动化测试

接口相关链接

接口自动化相关链接

ppt
requests 官网链接
宠物商店

宠物商店练习

import requests
from requests import Session


class TestPet:
    def setup(self):
        self.proxys = {
            "http": "127.0.0.1:8888",
            "https": "127.0.0.1:8888"
        }
        self.verify = False
        # session = Session()
        # session.proxies = {
        #     "http": "127.0.0.1:8888",
        #     "https": "127.0.0.1:8888"
        # }
        # session.verify = False

    def test_pet_get(self):
        # 获取宠物信息
        pet_get_url = "https://petstore.swagger.io/v2/pet/findByStatus"
        url_param = {
            "status": "available"
        }
        resp = requests.get(pet_get_url, url_param, verify=self.verify, proxies=self.proxys)
        print(resp.json())

    def test_pet_add(self):
        # 添加宠物信息
        pet_add_url = "https://petstore.swagger.io/v2/pet"
        pet_add_json = {
            "id": 0,
            "category": {
                "id": 0,
                "name": "string"
            },
            "name": "doggie202310121637",
            "photoUrls": [
                "string"
            ],
            "tags": [
                {
                    "id": 0,
                    "name": "string"
                }
            ],
            "status": "available"
        }
        resp = requests.post(pet_add_url,json=pet_add_json,verify=self.verify, proxies=self.proxys)
        print(resp.json())

    def test_pet_update(self):
        # 更新宠物信息
        pet_update_url = "https://petstore.swagger.io/v2/pet"
        pet_update_json = {
            "id": 9222968140497180504,
            "category": {
                "id": 0,
                "name": "string"
            },
            "name": "dog",
            "photoUrls": [
                "string"
            ],
            "tags": [
                {
                    "id": 0,
                    "name": "string"
                }
            ],
            "status": "available"
        }
        resp = requests.put(pet_update_url,json=pet_update_json,verify=self.verify, proxies=self.proxys)
        print(resp.json())

    def test_pet_delete(self):
        # 删除宠物信息
        pet_id = "9222968140497180504"
        pet_delete_url = f"https://petstore.swagger.io/v2/pet/{pet_id}"
        resp = requests.delete(pet_delete_url,verify=self.verify, proxies=self.proxys)
        print(resp.json())

待优化点

  1. 提取pet json
  2. 提取base url
  3. 测试断言assert

202041412220 区世钧
各种属性的封装
F3~%}DY~K)F@@V7_GV`WAB
各种功能:与断言

K4GCE@BN%4Y7~9B7IN((67

202041404124王沛维

import requests
from requests import Session

class TestPet:

def setup(self):
    self.session = Session()
    self.session.verify = False
    self.base_url = "https://petstore.swagger.io/v2/pet"
    self.base_json = {
        "id": 0,
        "category": {
            "id": 0,
            "name": "string"
        },
        "name": "",
        "photoUrls": [
            "string"
        ],
        "tags": [
            {
                "id": 0,
                "name": "string"
            }
        ],
        "status": "available"
    }

def test_pet_get(self):
    pet_get_url = f"{self.base_url}/findByStatus"
    url_param = {
        "status": "available",
    }
    resp = self.session.get(pet_get_url, params=url_param)
    print(resp.text)

def test_pet_add(self):
    # 添加宠物信息
    pet_add_url = self.base_url
    pet_add_json = self.base_json
    pet_add_json["name"] = "dog123"
    resp = self.session.post(pet_add_url, json=pet_add_json)
    assert resp.status_code == 200

def test_pet_update(self):
    pet_add_url = self.base_url
    pet_add_json = self.base_json
    pet_add_json["name"] = "dog123"
    pet_id = self.session.post(pet_add_url, json=pet_add_json).json()['id']

    # 更新宠物信息
    pet_update_url = self.base_url
    pet_update_json = self.base_json
    pet_update_json['id'] = pet_id
    pet_update_json['name'] = "dog"
    resp = self.session.put(pet_update_url, json=pet_update_json, verify=False)
    assert resp.json()['name'] == "dog"

def test_pet_delete(self):
    pet_add_url = self.base_url
    pet_add_json = self.base_json
    pet_add_json["name"] = "dog123"
    pet_id = self.session.post(pet_add_url, json=pet_add_json).json()['id']

    # 删除宠物信息
    pet_delete_url = f"{self.base_url}/{pet_id}"
    resp = self.session.delete(pet_delete_url)
    assert resp.status_code == 200

202041412218 倪灿烽
test_pet.py

import requests
from requests import Session

class TestPet:
    def setup(self):
        self.proxys = {
            "http": "127.0.0.1:8888",
            "https": "127.0.0.1:8888"
        }
        self.pet_json = {
            "id": 202041412218,
            "category": {
                "id": 0,
                "name": "string"
            },
            "name": "ncf202041412218",
            "photoUrls": [
                "string"
            ],
            "tags": [
                {
                    "id": 0,
                    "name": "string"
                }
            ],
            "status": "available"
        }
        self.base_url = "https://petstore.swagger.io/v2/pet"
        self.verify = False
        # self.verify = False
        # self.session = Session()
        # self.session.proxies = {
        #
        # }
        # self.session.verify = False

    def test_pet_get(self):
        # 获取宠物信息
        except_number = 10
        pet_get_url = f"{self.base_url}/findByStatus"
        url_param = {
            "status": "available"
        }
        resp = requests.get(pet_get_url, url_param, verify=self.verify, proxies=self.proxys)
        assert len(resp.json()) >= except_number

    def test_pet_add(self):
        # 添加宠物信息
        pet_add_url = self.base_url
        pet_add_json = self.pet_json.copy()
        dog_name = "ncf202041412218"
        pet_add_json["name"] = dog_name
        resp = requests.post(pet_add_url, json=pet_add_json, verify=self.verify, proxies=self.proxys)
        assert resp.json()["name"] == dog_name

    def test_pet_update(self):
        # 更新宠物信息
        pet_update_url = self.base_url
        pet_update_json = self.pet_json.copy()
        dog_name_update = "dog"
        # 新增宠物用于更新
        pet_add_json = self.pet_json.copy()
        pet_add_json["name"] = "ncf202041412218"
        pet_id = requests.post(self.base_url, json=pet_add_json, verify=self.verify, proxies=self.proxys).json()["id"]
        # 根据用户id修改用户name
        pet_update_json["id"] = pet_id
        pet_update_json["name"] = dog_name_update
        resp = requests.put(pet_update_url, json=pet_update_json, verify=self.verify, proxies=self.proxys)
        assert resp.json()["name"] == dog_name_update

    def test_pet_delete(self):
        # 新增宠物用于更新
        pet_add_json = self.pet_json.copy()
        pet_add_json["name"] = "ncf202041412218"
        pet_id = requests.post(self.base_url, json=pet_add_json, verify=self.verify, proxies=self.proxys).json()["id"]
        # 删除宠物信息
        pet_id = pet_id
        pet_delete_url = f"{self.base_url}/{pet_id}"
        resp = requests.delete(pet_delete_url, verify=self.verify, proxies=self.proxys)
        # 判断是否删除成功
        assert resp.json()["message"] == str(pet_id)

202041412221 阮凯林

test_pet.py

import requests


class TestPet:

    def setup(self):
        self.base_url = "https://petstore.swagger.io/v2/pet"
        self.verify = False
        self.pet_json = {
            "id": 202041412221,
            "category": {
                "id": 0,
                "name": "string"
            },
            "name": "202041412221rkl",
            "photoUrls": [
                "string"
            ],
            "tags": [
                {
                    "id": 0,
                    "name": "string"
                }
            ],
            "status": "available"
        }

    # 获取宠物信息
    def test_pet_get(self):
        except_number = 10
        pet_get_url = f"{self.base_url}/findByStatus"
        url_param = {
            "status": "available"
        }
        response = requests.get(pet_get_url, url_param, verify=self.verify)
        assert len(response.json()) >= except_number

    # 添加宠物信息
    def test_pet_add(self):
        pet_add_url = self.base_url
        pet_add_json = self.pet_json.copy()
        dog_name = "202041412221rkl"
        pet_add_json["name"] = dog_name
        response = requests.post(pet_add_url, json=pet_add_json, verify=self.verify)
        assert response.json()["name"] == dog_name

    # 更新宠物信息
    def test_pet_update(self):
        pet_update_url = self.base_url
        pet_update_json = self.pet_json.copy()
        dog_name_update = "dog"
        # 新增宠物用于更新
        pet_add_json = self.pet_json.copy()
        pet_add_json["name"] = "202041412221rkl"
        pet_id = requests.post(self.base_url, json=pet_add_json, verify=self.verify).json()["id"]
        # 根据用户id修改用户name
        pet_update_json["id"] = pet_id
        pet_update_json["name"] = dog_name_update
        response = requests.put(pet_update_url, json=pet_update_json, verify=self.verify)
        assert response.json()["name"] == dog_name_update

    # 新增宠物用于更新
    def test_pet_delete(self):
        pet_add_json = self.pet_json.copy()
        pet_add_json["name"] = "202041412221rkl"
        pet_id = requests.post(self.base_url, json=pet_add_json, verify=self.verify).json()["id"]
        # 删除宠物信息
        pet_id = pet_id
        pet_delete_url = f"{self.base_url}/{pet_id}"
        response = requests.delete(pet_delete_url, verify=self.verify)
        # 判断是否删除成功
        assert response.json()["message"] == str(pet_id)

202041412208 蒋冠岳
test_pet.py

import requests as requests
from requests import Session


class TestPet:
    def setup(self):
        # session = Session()
        # session.proxies = {"http": "127.0.0.1:8888", "https": "127.0.0.1:8888"}
        # session.verify = False
        self.proxys = {"http": "127.0.0.1:8888", "https": "127.0.0.1:8888"}
        self.verify = False
        self.base_url = "https://petstore.swagger.io/v2/pet"

        self.session = Session()
        self.session.proxies = {"http": "127.0.0.1:8888", "https": "127.0.0.1:8888"}
        self.session.verify = False

        self.pet_json = {
            "id": 0,
            "category": {
                "id": 0,
                "name": "string"
            },
            "name": "doggie",
            "photoUrls": [
                "string"
            ],
            "tags": [
                {
                    "id": 0,
                    "name": "string"
                }
            ],
            "status": "available"
        }

    def test_get_status(self):
        # url_param = {
        #     "status": "available"
        # }
        param = "pending"
        explict = 3
        pet_get_url = f"{self.base_url}/findByStatus?status={param}"
        resp = self.session.get(pet_get_url)
        assert len(resp.json()) >= explict

    def test_pet_add(self):
        dog_name_add = "dogguan"
        pet_add_url = f"{self.base_url}"
        pet_add_json = self.pet_json.copy()
        pet_add_json["name"] = dog_name_add
        resp = self.session.post(pet_add_url, json=pet_add_json, verify=self.verify, proxies=self.proxys)
        assert resp.json()["name"] == dog_name_add

    def test_pet_update(self):
        pet_update_url = f"{self.base_url}"
        pet_update_json = self.pet_json.copy()
        dog_name_update = "123"

        # 新增
        pet_add_json = self.pet_json.copy()
        pet_add_json["name"] = "dogguan"
        pet_id = self.session.post(self.base_url, json=pet_add_json).json()["id"]

        pet_update_json["id"] = pet_id
        pet_update_json["name"] = dog_name_update
        # resp = requests.put(pet_update_url, json=pet_update_json, verify=self.verify, proxies=self.proxys)
        resp = self.session.put(pet_update_url, json=pet_update_json, verify=self.verify, proxies=self.proxys)
        assert resp.json()["name"] == dog_name_update

    def test_pet_delete(self):
        pet_add_json = self.pet_json.copy()
        pet_add_json["name"] = "dogguan"
        pet_id = self.session.post(self.base_url, json=pet_add_json).json()["id"]

        pet_delete_url = f"{self.base_url}/{pet_id}"
        resp = self.session.delete(pet_delete_url, verify=self.verify, proxies=self.proxys)
        assert resp.json()["message"] == str(pet_id)

202041404121 阮达

import requests
from requests import Session


class TestPet:
    def setup(self):
        self.session = Session()
        self.session.verify = False
        self.base_url = "https://petstore.swagger.io/v2/pet"
        self.base_json = {
            "id": 202041404121,
            "category": {
                "id": 0,
                "name": "string"
            },
            "name": "",
            "photoUrls": [
                "string"
            ],
            "tags": [
                {
                    "id": 0,
                    "name": "string"
                }
            ],
            "status": "available"
        }

    def test_pet_get(self):
        pet_get_url = f"{self.base_url}/findByStatus"
        url_param = {
            "status": "available",
        }
        resp = self.session.get(pet_get_url, params=url_param)
        print(resp.text)

    def test_pet_add(self):
        # 添加宠物信息
        pet_add_url = self.base_url
        pet_add_json = self.base_json.copy()
        pet_add_json["name"] = "ruan202041404121"
        resp = self.session.post(pet_add_url, json=pet_add_json)
        assert resp.status_code == 200

    def test_pet_update(self):
        pet_add_url = self.base_url
        pet_add_json = self.base_json.copy()
        pet_add_json["name"] = "202041404121"
        pet_id = self.session.post(pet_add_url, json=pet_add_json).json()['id']

        # 更新宠物信息
        pet_update_url = self.base_url
        pet_update_json = self.base_json.copy()
        pet_update_json['id'] = pet_id
        pet_update_json['name'] = "ruan202041404121"
        resp = self.session.put(pet_update_url, json=pet_update_json, verify=False)
        assert resp.json()['name'] == 'ruan202041404121'

    def test_pet_delete(self):
        pet_add_url = self.base_url
        pet_add_json = self.base_json.copy()
        pet_add_json["name"] = "ruan202041404121"
        pet_id = self.session.post(pet_add_url, json=pet_add_json).json()['id']

        # 删除宠物信息
        pet_delete_url = f"{self.base_url}/{pet_id}"
        resp = self.session.delete(pet_delete_url)
        assert resp.status_code == 200

202041412126 于智康

import os

import requests
from requests import Session


class TestPet:
    def setup(self):
        # self.proxys = {
        #     "http": "127.0.0.1:8888",
        #     "https": "127.0.0.1:8888"
        # }
        # self.verify = False
        self.pet_json = {
            "id": 0,
            "category": {
                "id": 0,
                "name": "string"
            },
            "name": "",
            "photoUrls": [
                "string"
            ],
            "tags": [
                {
                    "id": 0,
                    "name": "string"
                }
            ],
            "status": "available"
        }
        self.base_url = "https://petstore.swagger.io/v2/pet"
        self.session = requests.Session()
        self.session.proxies = {
            'http': "127.0.0.1:8888",
            'https': "127.0.0.1:8888"
        }

        self.session.verify = False

    def test_pet_get(self):
        # 获取宠物信息
        pet_get_url = f"{self.base_url}/findByStatus"
        url_param = {
            "status": "available"
        }
        resp = requests.get(pet_get_url, url_param, verify=self.session.verify, proxies=self.session.proxies)
        print(resp.json())

    def test_pet_add(self):
        # 添加宠物信息
        pet_add_url = self.base_url
        pet_add_json = self.pet_json.copy()
        pet_add_json["name"] = "dog1750"
        resp = requests.post(pet_add_url, json=pet_add_json, verify=self.session.verify, proxies=self.session.proxies)
        print(resp.json())

    def test_pet_update(self):
        # 更新宠物信息
        pet_update_url = self.base_url
        pet_update_json = self.pet_json.copy()
        dog_name_upadte = "dog"

        # 新增宠物用于更新
        pet_add_json = self.pet_json.copy()
        pet_add_json["name"] = "dog1750"
        pet_id = requests.post(self.base_url, json=pet_add_json, verify=self.session.verify,
                               proxies=self.session.proxies).json()["id"]
        # 根据新增的用户id进行更改
        pet_update_json["id"] = pet_id
        pet_update_json["name"] = dog_name_upadte
        resp = requests.put(pet_update_url, json=pet_update_json, verify=self.session.verify,
                            proxies=self.session.proxies)
        assert resp.json()["name"] == dog_name_upadte

    def test_pet_delete(self):
        # 新增宠物用于更新
        pet_add_json = self.pet_json.copy()
        pet_add_json["name"] = "dog1750"
        pet_id = requests.post(self.base_url, json=pet_add_json, verify=self.session.verify,
                               proxies=self.session.proxies).json()["id"]
        # 删除宠物信息
        pet_id = pet_id
        pet_delete_url = f"{self.base_url}/{pet_id}"
        resp = requests.delete(pet_delete_url, verify=self.session.verify, proxies=self.session.proxies)
        assert resp.json()["message"] == str(pet_id)

名字:翁开锋 学号:202041412223

import requests
from requests import Session


class TestPet:
    def setup(self):
        # self.proxys = {
        #     "http": "127.0.0.1:8888",
        #     "https": "127.0.0.1:8888"
        # }
        self.pet_json = {
            "id": 202041412223,
            "category": {
                "id": 0,
                "name": "string"
            },
            "name": "wkf20204141223",
            "photoUrls": [
                "string"
            ],
            "tags":[
                {
                    "id": 0,
                    "name": "string"
                }
            ],
            "status": "available"
        }
        self.verify = False
        # session = Session()
        # session.proxies = {
        #     "http": "127.0.0.1:8888",
        #     "https": "127.0.0.1:8888"
        # }
        # session.verify = False

    def test_pet_get(self):
        # 获取宠物信息
        except_number = 10
        pet_get_url = "https://petstore.swagger.io/v2/pet/findByStatus"
        url_param = {
            "status": "available"
        }
        resp = requests.get(pet_get_url, url_param, verify=self.verify)
        print(resp.json())
        assert  len(resp.json()) >= except_number

    def test_pet_add(self):
        # 添加宠物信息
        pet_add_url = "https://petstore.swagger.io/v2/pet"
        pet_add_json = self.pet_json.copy()
        dog_name = "wkf202041412223"
        pet_add_json["name"] = dog_name
        resp = requests.post(pet_add_url,json=pet_add_json,verify=self.verify)
        print(resp.json())
        assert resp.json()["name"] == dog_name

    def test_pet_update(self):
        # 更新宠物信息
        pet_update_url = "https://petstore.swagger.io/v2/pet"
        pet_update_json = self.pet_json.copy()
        dog_name_update = "dog"

        pet_add_json = self.pet_json.copy()
        pet_add_json["name"] = "wkf202041412223"
        pet_id = requests.post(pet_update_url, json=pet_add_json,verify=self.verify).json()["id"]

        pet_update_json["id"] = pet_id
        pet_update_json["name"] = dog_name_update
        resp = requests.put(pet_update_url,json=pet_update_json,verify=self.verify)
        print(resp.json())
        assert  resp.json()["name"] == dog_name_update

    def test_pet_delete(self):

        pet_add_json = self.pet_json.copy()
        pet_add_url = "https://petstore.swagger.io/v2/pet"
        pet_add_json["name"] = "wkf202041412223"
        pet_id = requests.post(pet_add_url, json=pet_add_json, verify=self.verify).json()["id"]
        # 删除宠物信息
        # pet_id = "9222968140497180504"
        pet_delete_url = f"{pet_add_url}/{pet_id}"
        resp = requests.delete(pet_delete_url,verify=self.verify)
        print(resp.json())
        assert resp.json()["message"] == str(pet_id)


林梓豪 202041412213

import requests
from requests import Session


class TestPet:
    def setup(self):
        # session = Session()
        # session.proxies = {
        #     "http":"127.0.0.1:8888",
        #     "https":"127.0.0.1:8888"
        # }
        # session.verify = False
        self.pet_json = {

            "id": 0,
            "category": {
                "id": 0,
                "name": "string"
            },
            "name": "dog202041412213",
            "photoUrls": [
                "string"
            ],
            "tags": [
                {
                    "id": 0,
                    "name": "string"
                }
            ],
            "status": "available"
        }
        self.base_url = "https://petstore.swagger.io/v2/pet"
        self.proxys = {
            "http": "127.0.0.1:8888",
            "https": "127.0.0.1:8888"
        }
        self.verify = False

    def test_pet_get(self):
        # 获取宠物信息
        # expect_number = 10
        # pet_get_url = f"{self.base_url}/findByStatus"
        # url_param = {
        #     "status": "available"
        # }
        # resp = requests.get(pet_get_url, url_param, verify=self.verify, proxies=self.proxys)
        # assert len(resp.json()) >= expect_number
        except_number = 10
        pet_get_url = f"{self.base_url}/findByStatus"
        url_param = {
            "status": "available"
        }
        resp = requests.get(pet_get_url, url_param, verify=self.verify, proxies=self.proxys)
        assert len(resp.json()) >= except_number

    def test_pet_add(self):
        pet_add_url = self.base_url
        pet_add_json = self.pet_json.copy()
        add_dog_name = "dog2213"
        pet_add_json["name"] = add_dog_name
        resp = requests.post(url=pet_add_url, json=pet_add_json, verify=self.verify, proxies=self.proxys)
        assert resp.json()["name"] == add_dog_name

    def test_pet_update(self):
        # 更新宠物信息
        pet_update_url = self.base_url
        pet_update_json = self.pet_json.copy()
        update_dog_name = "dog"
        # 添加宠物
        pet_add_json = self.pet_json.copy()
        pet_add_json["name"] = "dog2213"
        pet_id = requests.post(self.base_url, json=pet_add_json, verify=self.verify, proxies=self.proxys).json()["id"]

        pet_update_json["id"] = pet_id
        pet_update_json["name"] = update_dog_name
        resp = requests.put(pet_update_url, json=pet_update_json, verify=self.verify, proxies=self.proxys)
        assert resp.json()["name"] == update_dog_name

    def test_pet_delete(self):
        pet_add_json = self.pet_json.copy()
        pet_add_json["name"] = "dog2213"
        pet_id = requests.post(self.base_url, json=pet_add_json, verify=self.verify, proxies=self.proxys).json()[
            "id"]
        pet_id = pet_id
        pet_delete_url = f"{self.base_url}/{pet_id}"
        resp = requests.delete(pet_delete_url, verify=self.verify, proxies=self.proxys)
        assert resp.json()["message"] == str(pet_id)

202041404129 詹佳润
python
import requests
from requests import Session
import json

class TestPet:
    def setup(self):
        self.proxies = {
            "http": "127.0.0.1:8888",
            "https": "127.0.0.1:8888"
        }
        self.verify = False
        self.base_url = "https://petstore.swagger.io/v2/pet"
        
        self.pet_json = {
            "id": 0,
            "category": {
                "id": 0,
                "name": "string"
            },
            "name": "doggie202310121637",
            "photoUrls": ["string"],
            "tags": [{"id": 0, "name": "string"}],
            "status": "available"
        }

    def test_pet_get(self):
        # 获取宠物信息
        pet_get_url = f"{self.base_url}/findByStatus"
        url_param = {"status": "available"}
        resp = requests.get(pet_get_url, params=url_param, verify=self.verify, proxies=self.proxies)
        data = resp.json()
        assert resp.status_code == 200, f"Expected status code 200, but got {resp.status_code}"
        # You can add more assertions here to check the content if needed
        print(data)

    def test_pet_add(self):
        # 添加宠物信息
        pet_add_url = self.base_url
        resp = requests.post(pet_add_url, json=self.pet_json, verify=self.verify, proxies=self.proxies)
        data = resp.json()
        assert resp.status_code == 200, f"Expected status code 200, but got {resp.status_code}"
        # You can add more assertions here to check the content if needed
        print(data)

    def test_pet_update(self):
        # 更新宠物信息
        pet_update_url = self.base_url
        pet_update_json = self.pet_json
        pet_update_json["name"] = "dog"
        resp = requests.put(pet_update_url, json=pet_update_json, verify=self.verify, proxies=self.proxies)
        data = resp.json()
        assert resp.status_code == 200, f"Expected status code 200, but got {resp.status_code}"
        # You can add more assertions here to check the content if needed
        print(data)

    def test_pet_delete(self):
        # 删除宠物信息
        pet_id = "9222968140497180504"
        pet_delete_url = f"{self.base_url}/{pet_id}"
        resp = requests.delete(pet_delete_url, verify=self.verify, proxies=self.proxies)
        data = resp.json()
        assert resp.status_code == 200, f"Expected status code 200, but got {resp.status_code}"
        # You can add more assertions here to check the content if needed
        print(data)

刘尚杰 202041412216

import requests
from requests import Session


class TestPet:
    def setup(self):
        # self.proxys = {
        #     "http": "127.0.0.1:8888",
        #     "https": "127.0.0.1:8888"
        # }
        self.pet_json = {
            "id": 202041412216,
            "category": {
                "id": 0,
                "name": "string"
            },
            "name": "Lsj202041412216",
            "photoUrls": [
                "string"
            ],
            "tags":[
                {
                    "id": 0,
                    "name": "string"
                }
            ],
            "status": "available"
        }
        self.verify = False
        # session = Session()
        # session.proxies = {
        #     "http": "127.0.0.1:8888",
        #     "https": "127.0.0.1:8888"
        # }
        # session.verify = False

    def test_pet_get(self):
        # 获取宠物信息
        except_number = 10
        pet_get_url = "https://petstore.swagger.io/v2/pet/findByStatus"
        url_param = {
            "status": "available"
        }
        resp = requests.get(pet_get_url, url_param, verify=self.verify)
        assert  len(resp.json()) >= except_number

    def test_pet_add(self):
        # 添加宠物信息
        pet_add_url = "https://petstore.swagger.io/v2/pet"
        pet_add_json = self.pet_json.copy()
        dog_name = "Lsj202041412216"
        pet_add_json["name"] = dog_name
        resp = requests.post(pet_add_url,json=pet_add_json,verify=self.verify)
        assert resp.json()["name"] == dog_name

    def test_pet_update(self):
        # 更新宠物信息
        pet_update_url = "https://petstore.swagger.io/v2/pet"
        pet_update_json = self.pet_json.copy()
        dog_name_update = "dog"

        pet_add_json = self.pet_json.copy()
        pet_add_json["name"] = "Lsj202041412216"
        pet_id = requests.post(pet_update_url, json=pet_add_json,verify=self.verify).json()["id"]

        pet_update_json["id"] = pet_id
        pet_update_json["name"] = dog_name_update
        resp = requests.put(pet_update_url,json=pet_update_json,verify=self.verify)
        assert  resp.json()["name"] == dog_name_update

    def test_pet_delete(self):

        pet_add_json = self.pet_json.copy()
        pet_add_url = "https://petstore.swagger.io/v2/pet"
        pet_add_json["name"] = "Lsj202041412216"
        pet_id = requests.post(pet_add_url, json=pet_add_json, verify=self.verify).json()["id"]
        # 删除宠物信息
        # pet_id = "9222968140497180504"
        pet_delete_url = f"{pet_add_url}/{pet_id}"
        resp = requests.delete(pet_delete_url,verify=self.verify)
        assert resp.json()["message"] == str(pet_id)

202041412102甘胜华

import requests
from requests import Session


class TestPet:
    def setup(self):
        self.base_url = "https://petstore.swagger.io/v2/pet"
        self.pet_json = {
            "id": 0,
            "category": {
                "id": 0,
                "name": "string"
            },
            "name": "",
            "photoUrls": [
                "string"
            ],
            "tags": [
                {
                    "id": 0,
                    "name": "string"
                }
            ],
            "status": "available"
        }
        self.proxys = {
            "http": "127.0.0.1:8888",
            "https": "127.0.0.1:8888"
        }
        self.verify = False
        # self.session = Session()
        # self.session.proxies = {
        #     "http": "127.0.0.1:8888",
        #     "https": "127.0.0.1:8888"
        # }
        # self.session.verify = False

    def test_pet_get(self):
        # 获取宠物信息
        pet_get_url = self.base_url + "/findByStatus"
        url_param = {
            "status": "available"
        }
        resp = requests.get(pet_get_url, url_param, verify=self.verify, proxies=self.proxys)
        assert resp.status_code == 200

    def test_pet_add(self):
        # 添加宠物信息
        pet_add_url = self.base_url
        #深拷贝
        pet_add_json = self.pet_json.copy()
        pet_add_json["name"] = "dog"
        resp = requests.post(pet_add_url,json=pet_add_json,verify=self.verify, proxies=self.proxys)
        assert resp.json()["name"] == pet_add_json["name"]

    def test_pet_update(self):
        # 更新宠物信息
        pet_update_url = self.base_url
        pet_update_json = self.pet_json.copy()
        pet_update_json["name"] = "dog and cat"

        pet_add_json = self.pet_json.copy()
        pet_update_json["id"] = requests.post(self.base_url, json=pet_add_json, verify=self.verify, proxies=self.proxys).json()["id"]

        resp = requests.put(pet_update_url,json=pet_update_json,verify=self.verify, proxies=self.proxys)
        assert resp.json()["name"] == pet_update_json["name"]

    def test_pet_delete(self):
        # 删除宠物信息
        pet_add_json = self.pet_json.copy()
        pet_id = requests.post(self.base_url, json= pet_add_json, verify=self.verify, proxies=self.proxys).json()["id"]

        pet_delete_url = self.base_url + f"/{pet_id}"
        resp = requests.delete(pet_delete_url,verify=self.verify, proxies=self.proxys)
        #message的id为string, pet_id为int
        assert resp.json()["message"] == str(pet_id)

姓名:胡福生 学号:202041404204

import requests
from requests import Session

class TestPet:
    def setup(self):
        self.session = requests.Session()
        self.session.proxys = {
            "http": "127.0.0.1:8888",
            "https": "127.0.0.1:8888"
        }
        self.session.verify = False
        self.base_url = "https://petstore.swagger.io/v2/pet"
        self.pet_json = {
            "id": 0,
            "category": {
                "id": 0,
                "name": "string"
            },
            "name": "",
            "photoUrls": [
                "string"
            ],
            "tags": [
                {
                    "id": 0,
                    "name": "string"
                }
            ],
            "status": "available"
        }


    def test_pet_get(self):
        # 获取宠物信息
        num=6
        pet_get_url = f"{self.base_url}/findByStatus"
        url_param = {
            "status": "available"
        }
        resp = requests.get(pet_get_url, url_param, verify=self.session.verify, proxies=self.session.proxies)
        assert len(resp.json())>=num

    def test_pet_add(self):
        # 添加宠物信息
        pet_add_url = self.base_url
        pet_add_json = self.pet_json
        pet_add_name="202041404204hfs"
        pet_add_json["name"]=pet_add_name
        resp = requests.post(pet_add_url,json=pet_add_json,verify=self.session.verify, proxies=self.session.proxies)
        assert resp.json()["name"]==pet_add_name

    def test_pet_update(self):
        # 添加宠物信息用于更新
        pet_add_url = self.base_url
        pet_add_json = self.pet_json
        pet_add_name = "202041404204hfs"
        pet_add_json["name"] = pet_add_name
        pet_id =requests.post(pet_add_url,json=pet_add_json,verify=self.session.verify, proxies=self.session.proxies).json()["id"]
        # 更新宠物信息
        pet_update_url = self.base_url
        pet_update_json = self.pet_json
        pet_update_name = "hfs202041404204"
        pet_update_json["id"] = pet_id
        pet_update_json["name"]=pet_update_name
        resp = requests.put(pet_update_url,json=pet_update_json,verify=self.session.verify, proxies=self.session.proxies)
        assert resp.json()["name"]==pet_update_name

    def test_pet_delete(self):
        # 添加宠物信息用于删除
        pet_add_url = self.base_url
        pet_add_json = self.pet_json
        pet_add_name = "202041404204hfs"
        pet_add_json["name"] = pet_add_name
        pet_id = requests.post(pet_add_url, json=pet_add_json, verify=self.session.verify, proxies=self.session.proxies).json()["id"]
        # 删除宠物信息
        pet_delete_url = f"{self.base_url}/{pet_id}"
        resp = requests.delete(pet_delete_url,verify=self.session.verify, proxies=self.session.proxies)
        assert resp.json()["message"] == str(pet_id)

202041404203 段杰东
import requests
from requests import Session

class Test_pet():
def setup(self):
self.proxys={
“http”: “127.0.0.1:8888”,
“https”: “127.0.0.1:8888”
}
self.verify=False
self.basr_url=“https://petstore.swagger.io/v2/pet
self.pet_json = {
“id”: 0,
“category”: {
“id”: 0,
“name”: “string”
},
“name”: “梧桐”,
“photoUrls”: [
“string”
],
“tags”: [
{
“id”: 0,
“name”: “string”
}
],
“status”: “available”
}
self.url_param = {
“status”: “available”
}
def test_pet_get(self):
# 获取宠物信息
pet_get_url = f"{self.basr_url}/findByStatus"
resp = requests.get(pet_get_url, self.url_param, verify=self.verify, proxies=self.proxys)
print(resp.json())
assert resp.status_code == 200
def test_pet_add(self):
# 添加宠物信息
pet_add_json = self.pet_json.copy()
pet_add_json[“id”] = 0
pet_add_json[“name”] = “monkey”
resp = requests.post(self.basr_url,json=pet_add_json,verify=self.verify, proxies=self.proxys)
print(resp.json())
assert resp.json()[“name”] == pet_add_json[“name”]

def test_pet_update(self):
    # 更新宠物信息

    pet_update_json=self.pet_json.copy()
    pet_update_json["id"]=559
    pet_update_json["name"]="dog"

    resp = requests.put(self.basr_url,json=pet_update_json,verify=self.verify, proxies=self.proxys)
    print(resp.json())
    assert resp.json()["name"]==pet_update_json["name"]

def test_pet_delete(self):
    # 删除宠物信息
    pet_add_json = self.pet_json.copy()
    pet_id = requests.post(self.basr_url, json=pet_add_json, verify=self.verify, proxies=self.proxys).json()["id"]
    pet_delete_url = f"{self.basr_url}/{pet_id}"
    resp = requests.delete(pet_delete_url,verify=self.verify, proxies=self.proxys)
    print(resp.json())
    assert resp.json()["message"]==str(pet_id)

202041404216 聂振枫

import requests

class TestPet:

def setup(self):
    self.base_url = "https://petstore.swagger.io/v2/pet"
    self.verify = False
    self.pet_json = {
        "id": 202041404216,
        "category": {
            "id": 0,
            "name": "string"
        },
        "name": "佩佩",
        "photoUrls": [
            "string"
        ],
        "tags": [
            {
                "id": 0,
                "name": "string"
            }
        ],
        "status": "available"
    }
    self.proxys = {
        "http":"127.0.0.1:8888",
        "https":"127.0.0.1:8888"
    }

# 获取宠物信息
def test_pet_get(self):
    except_number = 10
    pet_get_url = f"{self.base_url}/findByStatus"
    url_param = {
        "status": "available"
    }
    response = requests.get(pet_get_url, url_param, verify=self.verify, proxies=self.proxys)
    assert len(response.json()) >= except_number

# 添加宠物信息
def test_pet_add(self):
    pet_add_url = self.base_url
    pet_add_json = self.pet_json.copy()
    pet_name = "佩佩"
    pet_add_json["name"] = pet_name
    response = requests.post(pet_add_url, json=pet_add_json, verify=self.verify, proxies=self.proxys)
    assert response.json()["name"] == pet_name


# 新增宠物用于更新
def test_pet_delete(self):
    pet_add_json = self.pet_json.copy()
    pet_add_json["name"] = "佩佩"
    pet_id = requests.post(self.base_url, json=pet_add_json, verify=self.verify).json()["id"]
    # 删除宠物信息
    pet_id = pet_id
    pet_delete_url = f"{self.base_url}/{pet_id}"
    response = requests.delete(pet_delete_url, verify=self.verify, proxies=self.proxys)
    # 判断是否删除成功
    assert response.json()["message"] == str(pet_id)

结果:

姓名:王冠毅 学号:202041412222

test.py

proxys={"http":"127.0.0.1:8888",
            "https":"127.0.0.1:8888"}
class Test_Pet:
    def setup(self):
        self. session=Session()
        self.base_url="https://petstore.swagger.io/v2/pet"
        self.session.proxies = {
             "http": "127.0.0.1:8888",
             "https": "127.0.0.1:8888"
        }
        self.data_json={
            "id": 0,
            "category": {
                "id": 0,
                "name": "yuki"
            },
            "name": "doggie",
            "photoUrls": [
                "string"
            ],
            "tags": [
                {
                    "id": 0,
                    "name": "string"
                }
            ],
            "status": "available"


        }
        self.session.verify=False


    def test_pet_get_statu(self):
        #获取宠物信息
              pet_get_by_statu=f"{self.base_url}/findByStatus"
              url_param={
                "status" : "available"
               }
              resp=requests.get(pet_get_by_statu,url_param,verify=False,proxies=proxys)
              print(resp.json())



    def test_pet_add(self):
        pet_add_url = self.base_url

        pet_add_json={

                "id": 0,
                "category": {
                    "id": 0,
                    "name": "yuki"
                },
                "name": "doggie",
                "photoUrls": [
                    "string"
                ],
                "tags": [
                    {
                        "id": 0,
                        "name": "string"
                    }
                ],
                "status": "available"


        }
        add_json=self.data_json.copy()
        name="古龙"
        add_json['name']=name
        add_json['category']['name']="冰呪龙"
        resp=requests.post(pet_add_url , json=add_json,verify=False)
        assert resp.json()['name'] == name

    def test_pet_update(self):
        # 更新宠物信息
        pet_update_url = self.base_url

        add_json = self.data_json.copy()
        name = "古龙"
        add_json['name'] = name
        add_json['category']['name'] = "冰呪龙"
        # pet_ID=requests.post(self.base_url, json=add_json, verify=False).json()['id']

        #update_json=add_json.copy()

        update_json = requests.post(self.base_url, json=add_json, verify=False).json()
        name_update="灭尽龙"
        update_json['name']=name_update
        update_json['category']['name']="黑龙"
        NAME = requests.put(pet_update_url, json=update_json, verify=False, proxies=proxys).json()['name']

        assert name_update==NAME

    def test_pet_delete(self):
        # 删除宠物信息
        pet_id = "9222968140497180504"
        pet_delete_url = f"https://petstore.swagger.io/v2/pet/{pet_id}"
        resp = requests.delete(pet_delete_url,verify=False, proxies=proxys)
        print(resp.json())

学号:202041404110 姓名:胡鑫

test_pet.py

import os
import requests


class TestPet:
    def setup(self):
        # self.proxy = {
        #     "http": "127.0.0.1:8888",
        #     "https": "127.0.0.1:8888"
        # }
        self.base_url = "https://petstore.swagger.io/v2/pet"
        self.session = requests.Session()
        os.environ['http_proxy'] = "127.0.0.1:8888"
        os.environ['https_proxy'] = "127.0.0.1:8888"
        self.session.verify = False
        self.pet_json = {
            "id": 0,
            "category": {
                "id": 0,
                "name": "string"
            },
            "name": "",
            "photoUrls": [
                "string"
            ],
            "tags": [
                {
                    "id": 0,
                    "name": "string"
                }
            ],
            "status": "available"
        }

    def test_pet_get(self):
        # 获取宠物信息
        except_number = 0
        pet_get_url = f"{self.base_url}/findByStatus"
        url_param = {
            "status": "available"
        }
        resp = self.session.get(pet_get_url, params=url_param, verify=self.session.verify, proxies=os.environ)
        print(resp.json())
        assert len(resp.json()) > except_number

    def test_pet_add(self):
        # 添加宠物信息
        pet_add_url = self.base_url
        pet_add_json = self.pet_json.copy()
        pet_name = "dog20231012211805"
        pet_add_json['name'] = pet_name
        resp = self.session.post(pet_add_url, json=pet_add_json, verify=self.session.verify, proxies=os.environ)
        print(resp.json())
        assert resp.json()['name'] == pet_name

    def test_pet_update(self):
        # 更新宠物信息
        pet_update_url = self.base_url
        pet_update_json = self.pet_json
        pet_update_name = "dog20231012213551"
        pet_update_json['name'] = pet_update_name
        pet_add_json = self.pet_json.copy()
        pet_name = "dog20231012213405"
        pet_add_json['name'] = pet_name
        resp = self.session.post(pet_update_url, json=pet_add_json, verify=self.session.verify, proxies=os.environ)
        pet_id = resp.json()['id']
        pet_update_json['id'] = pet_id
        resp = self.session.put(pet_update_url, json=pet_update_json, verify=self.session.verify, proxies=os.environ)
        print(resp.json())
        assert resp.json()['name'] == pet_update_name
        assert resp.json()['id'] == pet_id

    def test_pet_delete(self):
        # 删除宠物信息
        pet_add_url = self.base_url
        pet_add_json = self.pet_json.copy()
        pet_name = "dog20231012213605"
        pet_add_json['name'] = pet_name
        resp = self.session.post(pet_add_url, json=pet_add_json, verify=self.session.verify, proxies=os.environ)
        pet_id = resp.json()['id']
        pet_delete_url = f"{self.base_url}/{pet_id}"
        resp = self.session.delete(pet_delete_url, verify=self.session.verify, proxies=os.environ)
        print(resp.json())
        assert resp.json()['message'] == str(pet_id)



测试结果

202041412108赖畅宇
import requests
from requests import Session

class TestPet:
def setup(self):
self.proxys = {
“http”: “127.0.0.1:8888”,
“https”: “127.0.0.1:8888”
}
self.pet_json = {
“id”: 202041412108,
“category”: {
“id”: 0,
“name”: “string”
},
“name”: “lcy202041412108”,
“photoUrls”: [
“string”
],
“tags”: [
{
“id”: 0,
“name”: “string”
}
],
“status”: “available”
}
self.base_url = “https://petstore.swagger.io/v2/pet
self.verify = False

def test_pet_get(self):
    # 获取宠物信息
    except_number = 10
    pet_get_url = f"{self.base_url}/findByStatus"
    url_param = {
        "status": "available"
    }
    resp = requests.get(pet_get_url, url_param, verify=self.verify, proxies=self.proxys)
    assert len(resp.json()) >= except_number

def test_pet_add(self):
    # 添加宠物信息
    pet_add_url = self.base_url
    pet_add_json = self.pet_json.copy()
    dog_name = "lcy202041412108"
    pet_add_json["name"] = dog_name
    resp = requests.post(pet_add_url, json=pet_add_json, verify=self.verify, proxies=self.proxys)
    assert resp.json()["name"] == dog_name

def test_pet_update(self):
    # 更新宠物信息
    pet_update_url = self.base_url
    pet_update_json = self.pet_json.copy()
    dog_name_update = "dog"
    # 新增宠物用于更新
    pet_add_json = self.pet_json.copy()
    pet_add_json["name"] = "lcy202041412108"
    pet_id = requests.post(self.base_url, json=pet_add_json, verify=self.verify, proxies=self.proxys).json()["id"]
    # 根据用户id修改用户name
    pet_update_json["id"] = pet_id
    pet_update_json["name"] = dog_name_update
    resp = requests.put(pet_update_url, json=pet_update_json, verify=self.verify, proxies=self.proxys)
    assert resp.json()["name"] == dog_name_update

def test_pet_delete(self):
    # 新增宠物用于更新
    pet_add_json = self.pet_json.copy()
    pet_add_json["name"] = "lcy202041412108"
    pet_id = requests.post(self.base_url, json=pet_add_json, verify=self.verify, proxies=self.proxys).json()["id"]
    # 删除宠物信息
    pet_id = pet_id
    pet_delete_url = f"{self.base_url}/{pet_id}"
    resp = requests.delete(pet_delete_url, verify=self.verify, proxies=self.proxys)
    # 判断是否删除成功
    assert resp.json()["message"] == str(pet_id)

学号:202041404115
姓名:李升升
image