Mock-dependent interfaces using

What do mock
 1. front and rear ends of the FBI, if you are a front page development, now need to develop a feature: the next order, payment page interface, based on the result of the payment, the payment success, demonstrated successful payment page, failure to pay, failed to show payment page. To accomplish this function, you need to call back-end interface, based on the results back to you, to show different pages. At this point the back-end interfaces have not developed well, as a front-end developer can not wait for someone else to develop well, and then you develop, you only overtime fate. In order to synchronize the development to complete the task, then, you can in accordance with interface documentation, and the address of the interface and the parameters passed in the past, and then return to their own different mock interface screens to complete the front-end development tasks

 2. The purpose of unit testing, unit testing is to test the function of a small unit, but the function or method developed in reality there are dependencies, such as parameter b function, you need to call a function returns the result, but I As already tested a function of. In this case, do not need to test a function of time, this time can be used to simulate the call module mock this section, and returns the result given

 3. rely on third-party interfaces, making the interface automation of time, sometimes need to call a third-party interface, but other people's interface services beyond your control, there may be others provide testing services to today's environment you are driving, others will turn off, to the automation interface testing a lot of trouble, then you can write your own mock-server interface to simulate the return data.

1. The following scene: the payment is a separate interface provided by other development, according to the interface returns the status of the payment goes to show failure or success, you need to implement this function

That you write a function b, you write a colleague a function, you need to judge the b function according to the results of a function, and then implement corresponding functions. That's dependencies, the progress of the development of your colleagues that you have no control over if you wait for him to develop, and then you develop, that you sit and so on overtime now.

2. The following is to write zhifu_statues () function function, probably designed as follows, temple.py saved as a file.

 

 

Copy the code
# Saved as temple.py
#coding:utf-8

def zhifu ():
'' 'The assumption here is the function of a payment, undeveloped finish 
Payment successful return: { "result": "success", "reason": "null"} 
Payment fails to return: { "result": "fail", "reason": "lack of balance"} 
reason reason for the failure to return
'''
    pass

def zhifu_statues():
'' 'Is determined according to the results of payment or jump to the corresponding page' ''
    result = zhifu ()
    print(result)
    try:
        if result["result"] == "success":
            return "pay for success"
        elif result["result"] == "fail":
            print ( "failure reason:% s"% result [ "reason"])
            return "failure to pay"
        else:
            return "Unknown error exception."
    except:
     return "Error, the server returns an exception."
Copy the code

 

The following test unit is designed to:

Copy the code
# Saved as unittest_cases.py 

# Coding: UTF-8
from unittest import mock
import unittest
import temple

class Test_zhifu_statues(unittest.TestCase):
'' 'Unit test' ''
    def test_01(self):
'' 'Paid test success scenario' ''
#mock a successful payment data
        temple.zhifu = mock.Mock(return_value={"result": "success", "reason": "null"})
# Jump to pay according to the results of the test page
        statues = temple.zhifu_statues()
        print(statues)
        self.assertEqual (statues, "successful payment")
    def test_02(self):
'' 'Paid test failure scenarios' ''
#mock a failed payment data
        temple.zhifu = mock.Mock(return_value={"result": "fail", "reason": "余额不足"})
# Jump to pay according to the results of the test page
        statues = temple.zhifu_statues()
        print(statues)
        self.assertEqual (statues, 'failure to pay')
    def test_03(self):
        temple.zhifu = mock.Mock(return_value={"result": "1", "reason": "未知错误"})
        statues = temple.zhifu_statues()
        print(statues)
        self.assertEqual (statues, 'unknown error exception')

if __name__=="__main__":
    unittest.main()
Copy the code

Screenshot operating results:

Guess you like

Origin www.cnblogs.com/nadian/p/12061165.html