接口测试自动化脚本框架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,"用例测试不通过"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值