D91【python 接口自动化学习】- pytest基础用法

day91 pytest的setup,setdown详解(三)

学习日期:20241207

学习目标:pytest基础用法 -- pytest的setup,setdown详解(三)

学习笔记:

setup、teardown详解(三)
  • 类级 setup_class/teardown_class 只在类中前后运行一次(在类中)
python 复制代码
import requests
import pytest


class TestMobile:
    @classmethod
    def setup_class(cls):
        print("准备测试数据")

    @classmethod
    def teardown_class(cls):
        print("清理测试数据")

    def test_mobile(self):
        print("测试手机归属地get请求")
        r = requests.get('https://api.binstd.com/shouji/query',
                         params={"shouji": "13456755448", "appkey": "0c818521d38759e1"})
        assert r.status_code == 200
        result = r.json()
        assert result['status'] == 0
        assert result['msg'] == "ok"
        assert result['result']["shouji"] == "13456755448"
        assert result['result']["province"] == "浙江"
        assert result['result']["city"] == "杭州"
        assert result['result']["company"] == "中国移动"
        assert result['result']["cardtype"] is None
        assert result['result']["areacode"] == "0571"

    def test_mobile_post(self):
        print("测试手机归属地post请求")
        params = {
            "shouji": "13456755448",
            "appkey": "0c818521d38759e1"
        }
        r = requests.post('https://api.binstd.com/shouji/query', params=params)
        assert r.status_code == 200
        result = r.json()
        assert result['status'] == 0
        assert result['msg'] == "ok"
        assert result['result']["shouji"] == "13456755448"
        assert result['result']["province"] == "浙江"
        assert result['result']["city"] == "杭州"
        assert result['result']["company"] == "中国移动"
        assert result['result']["cardtype"] is None
        assert result['result']["areacode"] == "0571"


if __name__ == '__main__':
    pytest.main()
  • 方法级 setup_method/teardown_method 开始于方法始末(在类中)
python 复制代码
import requests
import pytest


class TestMobile:
    @classmethod
    def setup_method(cls):
        print("准备测试数据")

    @classmethod
    def teardown_method(cls):
        print("清理测试数据")

    def test_mobile(self):
        print("测试手机归属地get请求")
        r = requests.get('https://api.binstd.com/shouji/query',
                         params={"shouji": "13456755448", "appkey": "0c818521d38759e1"})
        assert r.status_code == 200
        result = r.json()
        assert result['status'] == 0
        assert result['msg'] == "ok"
        assert result['result']["shouji"] == "13456755448"
        assert result['result']["province"] == "浙江"
        assert result['result']["city"] == "杭州"
        assert result['result']["company"] == "中国移动"
        assert result['result']["cardtype"] is None
        assert result['result']["areacode"] == "0571"

    def test_mobile_post(self):
        print("测试手机归属地post请求")
        params = {
            "shouji": "13456755448",
            "appkey": "0c818521d38759e1"
        }
        r = requests.post('https://api.binstd.com/shouji/query', params=params)
        assert r.status_code == 200
        result = r.json()
        assert result['status'] == 0
        assert result['msg'] == "ok"
        assert result['result']["shouji"] == "13456755448"
        assert result['result']["province"] == "浙江"
        assert result['result']["city"] == "杭州"
        assert result['result']["company"] == "中国移动"
        assert result['result']["cardtype"] is None
        assert result['result']["areacode"] == "0571"


if __name__ == '__main__':
    pytest.main()
总结
  1. 类级 setup_class/teardown_class 只在类中前后运行一次(在类中)
  2. 方法级 setup_method/teardown_method 开始于方法始末(在类中)
相关推荐
用户27784491049937 小时前
借助DeepSeek智能生成测试用例:从提示词到Excel表格的全流程实践
人工智能·python
JavaEdge在掘金9 小时前
ssl.SSLCertVerificationError报错解决方案
python
我不会编程5559 小时前
Python Cookbook-5.1 对字典排序
开发语言·数据结构·python
老歌老听老掉牙10 小时前
平面旋转与交线投影夹角计算
python·线性代数·平面·sympy
满怀101510 小时前
Python入门(7):模块
python
无名之逆10 小时前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust
你觉得20510 小时前
哈尔滨工业大学DeepSeek公开课:探索大模型原理、技术与应用从GPT到DeepSeek|附视频与讲义下载方法
大数据·人工智能·python·gpt·学习·机器学习·aigc
啊喜拔牙10 小时前
1. hadoop 集群的常用命令
java·大数据·开发语言·python·scala
__lost11 小时前
Pysides6 Python3.10 Qt 画一个时钟
python·qt