In the automated testing process, we may encounter a A interface needs to call other interfaces (external interface), but other interfaces can not normally access; but we need to test the A interface; then we can use mock, to simulate a third party Interface after a successful request, access to his return value can be the next test
Payment third party interface to define classes
import requests class Payment: """ The definition of third-party payment categories """ @staticmethod def auth(card_num, amount): """ Request a payment interface treasure to pay third parties, not our own development interface : Param card_num: card number : Param amount: Amount : Return: the return status code, the code 200 successful payment, 500 delegates pay anomalies "" " URL = " HTTP: // string interfaces to third .site " # false address can not access the interface Data = { " card_num " : card_num, " AMOUNT " : AMOUNT} # parameters res = requests.post (url, data Data =) # parameter passing return res.status_code # normal return a status code, the code 200 successful payment, payment on behalf of abnormality 500 def pay(self, user_id, card_num, amount): """ payment method : Param user_id: user ID : Param card_num: card number : Param amount: Amount :return: """ try: status_code = self.auth (card_num, AMOUNT) the except TimeoutError: # timeout exception, and then pay status_code = self.auth (card_num, AMOUNT) IF status_code == 200: # returns 200 success Print (f " ! [paid] {user_id} {{amount}] success !!! debits and pay the registration record " ) return " Success " elif status_code == 500: # 500 delegates pay abnormal Print (f " [{user_id}] [{amount}} payment failure !!! without charge! " ) return " fail "
Next we tested payment interface
When to buy goods only after successful payment, but now can not call third-party payment interface, instead of on the use of mock successful recharge, convenient payment function test?
Import the unittest from the unittest Import the mock # introduced among mock unittest from scripts.bbpayment Import Payment # test payment interface module class PaymentTest(unittest.TestCase): """ Test payment interface """ def setUp(self): self.payment = Payment () # create objects of this class def test_normal(self): """ Normal call payment interface will fail :return: """ res = self.payment.pay(user_id=1001, card_num=6666, amount=100000) self.assertEqual('success', res) def test_success(self): """ Test successful payment, analog return value :return: "" " # Party payment interface can not access, we need to create a fake interface to replace it, the analog data returned # can return the object # self.payment.auth = mock.Mock (return_value = '{" name " : "beyond", "msg": "successful marriage", "code": 8888} ') # can simulate the return status code self.payment.auth = mock.Mock (return_value = 200) # create a sham to cover third-party interfaces interfaces, analog return 200 is RES = self.payment.pay (user_id = 1001 , card_num=6666, amount=100000) self.assertEqual('success', res) def test_fail(self): """ Test payment fails, analog return value :return: "" " Self.payment.auth = mock.Mock (return_value = 500) # create false interfaces to cover third-party interfaces, analog return 500 RES = self.payment.pay (user_id = 1001 , card_num=6666, amount=100000) self.assertEqual('Fail', res) def test_retry_success(self): """ After the third party interface test called a timeout, again string success, analog return value :return: "" " # Boundary check parameter passing self.payment.auth = mock.Mock (side_effect = [TimeoutError, 200 is ]) res = self.payment.pay(user_id=1001, card_num=6666, amount=100000) self.assertEqual('success', res) if __name__ == '__main__': unittest.main()
Fail results
Successful outcome
******* Please respect the original, as to reprint, please indicate the source: Reprinted from: https://www.cnblogs.com/shouhu/ Thank you! ! *******