请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

老师您讲解的mock的作用就是模拟返回值,但是我在学习的过程中发现老师mock的是请求参数data

import unittest
import requests
import json
from unittest import mock
class TestPush(unittest.TestCase):
def setUp(self):
print(‘case开始执行’)
def tearDown(self):
print(‘case执行结束’)
def post_request(self,url,data):
res=requests.post(url,data).json()
print (json.dumps(res,indent=4))
return json.dumps(res,indent=4)

def test_01(self):
    url_push='https://dj.palmestore.com/dj_push/out/upload/report'
    param_push={
        'appKey':'fe5fa891b376498c857e9e7dd080d2e2',
        'deviceToken':'CN_2eaf031de4c868bd8e83d6f2aecf1f52',
        'platform':'android',
        'pushChannel':'oppo',
        'sign':'r0jI8BET4Oy5mFxbN0KIMWA41dpgIB9UqnvvMXqeqmy/lclyHawPgoiSwXTf6uCK1BVYWrEzNiIJvZvPjaus6/9AugwFe45TsfLn8Mge/8MO9VrFLIosQ++YPXH5qaVPampBPyznzY15i07/wbsDcm+LXYmQsxdWNlmnuHdgNl0=',
        'timestamp':'1593861671148',
        'user':'j27566808',
        'pc':'10',
        'p2':'124011',
        'p3':'17124056',
        'p4':'501656',
        'p5':'16',
        'p6':'',	
        'p7':'__8b29c1f8c090c577',
        'p9':'2',
        'p12':'',	
        'p16':'OPPO A37m',
        'p21':'3',
        'p22':'5.1',
        'p25':'17124056',
        'p26':'22',
        'zyeid':'5d94300a-dcb1-48b5-be66-826b58623eac',
        'usr':'j27566808',
        'rgt':'7',
        'p1':'Wtbq+hAtvbEDAFr/gA/tJBmY',
        'ku':'j27566808',
        'kt':'520a979cc140a7bcf46af6a73787e7c7'
    }
    #res=self.post_request(url_push,param_push)
    sucess_test=mock.Mock(return_value=param_push)
    post_request=sucess_test
    res=post_request
    
    self.assertEqual({'appKey':"fe5fa891b376498c857e9e7dd080d2e2"},res())

if name==‘main’:
unittest.main()

正在回答 回答被采纳积分+3

2回答

Mushishi 2020-07-06 10:29:47

你上面提问的mock了。但是你下面回答的没有mock呀

0 回复 有任何疑惑可以回复我~
提问者 慕莱坞4356330 2020-07-05 11:35:07

from unittest import mock

import requests

import json


url_push='https://dj.palmestore.com/dj_push/out/upload/report'

param_push = {

    'appKey':'fe5fa891b376498c857e9e7dd080d2e2',

    'deviceToken':'CN_2eaf031de4c868bd8e83d6f2aecf1f52',

    'platform':'android',

    'pushChannel':'oppo',

    'sign':'r0jI8BET4Oy5mFxbN0KIMWA41dpgIB9UqnvvMXqeqmy/lclyHawPgoiSwXTf6uCK1BVYWrEzNiIJvZvPjaus6/9AugwFe45TsfLn8Mge/8MO9VrFLIosQ++YPXH5qaVPampBPyznzY15i07/wbsDcm+LXYmQsxdWNlmnuHdgNl0=',

    'timestamp':'1593861671148',

    'user':'j27566808',

    'pc':'10',

    'p2':'124011',

    'p3':'17124056',

    'p4':'501656',

    'p5':'16',

    'p6':'',    

    'p7':'__8b29c1f8c090c577',

    'p9':'2',

    'p12':'',   

    'p16':'OPPO A37m',

    'p21':'3',

    'p22':'5.1',

    'p25':'17124056',

    'p26':'22',

    'zyeid':'5d94300a-dcb1-48b5-be66-826b58623eac',

    'usr':'j27566808',

    'rgt':'7',

    'p1':'Wtbq+hAtvbEDAFr/gA/tJBmY',

    'ku':'j27566808',

    'kt':'520a979cc140a7bcf46af6a73787e7c7'

}


def post_request(url,data):

    res=requests.post(url,data).json()

    print (json.dumps(res,indent=4))

    return json.dumps(res,indent=4)

#post_request(url_BookUpdate,param_BookUpdate)

post_request(url_push,param_push)

这个post请求的实际返回值是:

https://img1.sycdn.imooc.com//szimg/5f0149ca09be276406120261.jpg

而我最上面mock的返回对象是 字典类型的参数

0 回复 有任何疑惑可以回复我~
  • def post_request(url,data):
    
        res=requests.post(url,data).json()
    
        print (json.dumps(res,indent=4))
    
        return json.dumps(res,indent=4)
    
    #post_request(url_BookUpdate,param_BookUpdate)
    
    post_request(url_push,param_push)  这个没有mock呀
    回复 有任何疑惑可以回复我~ 2020-07-06 10:29:57
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信