• 接口测试自动化脚本框架2


    框架:

    run.py

    import pytest
    import os
    
    if __name__ == '__main__':
    
       pytest.main()
    
       os.system("allure generate temp -o reports --clean")   #生成报告并且覆盖掉之前的报告

    pytest.ini

    [pytest]
    addopts=-v -s "test_cases\deletefile_moudle\test_file.py::Test_file::test_create" --alluredir=temp --clean-alluredir
    
    testpaths=./test_cases

    conftest.py

    import pytest
    import requests
    
    @pytest.fixture()     #声明一个fixture 希望它在测试用例执行之前,执行,login不再是普通函数,而是pytest的fixture
    def access_token():
        url = "http://120.24.208.55:81/?user/index/loginSubmit&name=admin&password=box2046"
    
        payload = {}
        headers = {
            'Cookie': 'CSRF_TOKEN=AueSvH9zycZpTDmu; KOD_SESSION_ID=112fe71aa47de18e72856efdf047ba77; kodUserID=1'
        }
    
        response = requests.post(url=url, headers=headers, data=payload).json()
    
        return response["info"]

    test_cases / deletefile_moudle / test_file.py

    import requests
    
    class Test_file:
        def test_create(self,access_token):
    
            url = "http://120.24.208.55:81/?explorer/index/mkdir"
    
            payload={"path":"{source:1}/222"}
    
            headers = {
                "access_token": f"{access_token}",
                'Content-Type': 'application/x-www-form-urlencoded',
                'Cookie': 'CSRF_TOKEN=AueSvH9zycZpTDmu; KOD_SESSION_ID=112fe71aa47de18e72856efdf047ba77; kodUserID=1'
            }
    
            response = requests.post(url=url, headers=headers, data=payload)
    
            global inf
            inf=response.json()["info"]
    
            #=================断言================================
            assert response.json()["code"]==True,"创建文件夹用例失败"
    
        def test_upload(self,access_token):
    
            url = "http://120.24.208.55:81/?explorer/upload/fileUpload"
    
            payload = {'path': f'{inf}'}
    
            files = [
                ('name', ('1.docx', open('/C:/Users/administraors/Desktop/1.docx', 'rb'),
                          'application/vnd.openxmlformats-officedocument.wordprocessingml.document'))
            ]
            headers = {
                'Cookie': 'CSRF_TOKEN=AueSvH9zycZpTDmu; KOD_SESSION_ID=112fe71aa47de18e72856efdf047ba77; kodUserID=1',
                'accessToken': f'{access_token}'
            }
    
            response = requests.post(url=url, headers=headers, data=payload, files=files)
    
            # =================断言================================
            assert response.json()["code"] ==True, "上传文件用例失败"
    
        def test_delete(self,access_token):
            pass
        def test_exit(self,access_token):
            pass

    test_cases / login_moudle / test_login.py

    import requests
    def test_login():
        url = "http://120.24.208.55:81/?user/index/loginSubmit&name=admin&password=box2046"
    
        payload={}
        headers = {
          'Cookie': 'CSRF_TOKEN=AueSvH9zycZpTDmu; KOD_SESSION_ID=112fe71aa47de18e72856efdf047ba77; kodUserID=1'
        }
    
        response = requests.post(url=url, headers=headers, data=payload)
    
        #===============断言=====================
        assert response.json()["code"]==True,"用例测试不通过"
    
  • 相关阅读:
    java8 Stream 常用的集合操作大全 Stream集合处理工具类整理
    【LeetCode】剑指 Offer <二刷>(5)
    到点了开始网抑云(悲)但是用python(整活)
    [Bread.Mvc] 开源一款自用 MVC 框架,支持 Native AOT
    字符串常用方法(1)
    物流供应商实现供应链自动化的3种方法
    基于逻辑规则的图谱推理
    Linux下MMDetection环境配置
    Python根据乐谱生成音乐
    51单片机数字电压表仿真设计_LCD显示
  • 原文地址:https://blog.csdn.net/liuaoxiang/article/details/126514218