pytest的方式运行完整项目实现接口自动化

在这里插入图片描述
首先需要使用python第三方模块

pip install pytest -i https://pypi.tuna.tsinghua.edu.cn/simple

安装之后cmd黑窗口输入pip list查看模块版本信息
打开pycharm软件配置环境
在这里插入图片描述
配置环境
在这里插入图片描述
我们newproject,然后创建文件夹,文件如图
在这里插入图片描述
在test_fw里面

from test_case.test_fw_login import *
from test_case.test_push_file import *
from test_case.test_edit_userinfo import *
class Test_Fw:
    #方维页面
    def setup_class(self):
        #pytest的前置操作 ---在所有的用例运行前必定会先运行
        self.s = test_fw_login()
        print('前置操作')
    def test_edit(self):
        #编辑个人资料
        test_edit_userinfo(self.s)

    def test_chongzhi(self):
        #充值上传图片
        test_push_file(self.s)

在test_fw_login

import requests
def test_fw_login():
    url='http://106.52.182.140/fanwe/index.php?ctl=user&act=dologin&fhash=xGhwpLIiqHoEAAvNeWFnikycDaFmbPBKfBSxMoNkNXJPdoRBSO'
    data={
        'email'	:'iop123',
        'user_pwd'	:'V2hrYmRzaXdhRG1ibFpaRnB5RXJKTGFoUmlvSFprWVlSdVZncEJDTm9yaW1QR0tNVnklMjV1NjVCOSUyNXU3RUY0aW9wMTIzJTI1dThGNkYlMjV1NEVGNg==',
        'ajax':	'1'
    }
    header={
        'Host': '106.52.182.140',
        'Origin': 'http://106.52.182.140',
        'Referer': 'http://106.52.182.140/fanwe/index.php?ctl=user&act=login'

    }
    s=requests.session()
    rq=s.post(url=url,data=data,headers=header)
    rsp=rq.content.decode('unicode_escape')
    print()
    print(rsp)
    return s

在test_edit_userinfo

import  requests
from  test_case.test_fw_login import test_fw_login
def test_edit_userinfo(s=None):
    if s==None:
        s=test_fw_login()
    edit_url='http://106.52.182.140/fanwe/member.php?ctl=uc_account&act=save'
    data_edit={
        'avatar_file': '',
        'graduation': '本科',
        'university': 'wuhan',
        'marriage': '已婚',
        'province_id': '2',
        'city_id': 52,
        'address': 'lifengfanclub',
        'phone': '17674128890-17674128890',
        'commit': '保存更改'
    }
    edit_header={
        'Referer': 'http://106.52.182.140/fanwe/member.php?ctl=uc_account'
    }
    rq=s.post(url=edit_url,data=data_edit,headers=edit_header)
    print()
    print('编辑个人资料接口的cookie{}'.format(s.cookies))
    print(rq.text[315:345])

在test_push_file

# D:\softTest\Python\work\filework\work
# C:\fakepath\lff1.png
import requests
from test_case.test_fw_login import test_fw_login

def test_push_file(s=None):
    if s == None:
        s = test_fw_login() #当没有给s传值的时候就自己给自己传值 --登录后的会话
    else:
        pass
    # 上传文件
    # 接口地址
    file_url = 'http://106.52.182.140/fanwe/file.php'
    # 请求数据
    file_data = {
        'a': 'do_upload',
        'localUrl': r'C:\fakepath\lff1.png',
        'm': 'File',
        'upload_type': '0'
    }
    # 文件信息
    path = r'D:\softTest\Python\work\filework\work\lff1.png'
    file = {
        'imgFile': ('lff1.png', open(path, 'rb'), 'image/png')  # tup.png --文件名称,open(path,'rb')--处理文件的方式
        # 'image/png' --文件类型
    }
    # 上传文件请求头
    file_header = {
        'Referer': 'http://106.52.182.140/fanwe/member.php?ctl=uc_money&act=incharge'
    }
    # 发起上传文件请求
    rq = s.post(url=file_url, data=file_data, files=file, headers=file_header)
    print()
    print('上传文件接口的cookie{}'.format(s.cookies))
    print(rq.text)  # 获取接口返回结果
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值