D98【python 接口自动化学习】- pytest进阶之fixture用法

发布于:2024-12-18 ⋅ 阅读:(117) ⋅ 点赞:(0)

day98 pytest的fixture功能之session

学习日期:20241215

学习目标:pytest基础用法 -- pytest的fixture功能之session

学习笔记:

fixture(scop="session")
  • (scop="session") 是多个文件调用一次,.py文件就是module
import pytest
import requests

@pytest.fixture(scope="module",autouse=True)
def func():
    print("我是前置步骤")

class TestClassFixture:
    def test_getmobile(self):
        print("测试get请求")
        params = {'key1': 'value1', 'key2': 'value2'}
        r=requests.get('https://httpbin.org/get',params=params)
        print(r.status_code)
        assert r.status_code == 200
        res = r.json()
        assert res['url'] == 'https://httpbin.org/get?key1=value1&key2=value2'
        assert res['origin'] == '163.125.202.248'
        assert res['args']['key1'] == 'value1'
        assert res['args']['key2'] == 'value2'

    def test_postmobile(self):
        print("测试post请求")
        params = {'key': 'value'}
        r = requests.post('https://httpbin.org/post', data=params)
        print(r.status_code)
        assert r.status_code == 200
        print(r.json())
        res=r.json()
        assert res['args'] == {}
        assert res['data'] == ''
        assert res['form']['key'] == 'value'

if __name__ == '__main__':
    pytest.main()
总结
  1. (scop="session") 是多个文件调用一次,.py文件就是module

网站公告

今日签到

点亮在社区的每一天
去签到