The method of testing Interface step by using unittest in python
Editor to share with you the method of python using unittest test interface step by step, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!
This time, we will give you a detailed explanation of python's use of unittest test interface. What are the considerations for python to use unittest test interface? here are the actual examples. Let's take a look.
1. First, use requests of python to test the interface
# TestInface.py import requests,json url = visit.get_test_url () news_url = url+'news.info' headers= baseToken.basetoken_datas () ['headers'] def new_data (data): r = requests.post (news_url,data=data,headers=headers) cnn = json.loads (r.text) return cnn
two。 Use unittest to call the interface and count the results of the interface test
# TestCase.py #-*-coding:utf-8-*-import unittest import TestInface # Statistics the case results executed # -- text = "" num_success = 0 num_fail = 0 # test passed def decide_success (joggle): global num_success num_success + = 1 print_out (joggle + ": interface test passed\ n") return num_success # test failed def decide_fail (txt) Joggle): global num_fail num_fail + = 1 print_out (joggle + ": interface test failed\ nError message:" + txt + "\ n") return num_fail # email content write & client output def print_out (message): global text text + = "\ n" + message return text # return value to judge def decide_result (result, code Joggle): if result ['code'] = = code: decide_success (joggle) return "pass" else: txt = u "expected return value:" + str (code) + u "actual return value:" + str (result) +'\ n' + result ['message'] decide_fail (txt, joggle) return "fail" def decide_Count (): data = {' num_success': num_success, 'num_fail': num_fail 'text': text} return data #- -# define unittest class MyTestCase (unittest.TestCase): # initialize work def setUp (self): pass # exit cleanup work def tearDown (self): pass def test_Case1 (self): id = 16 data = {'id':id} a = TestInface.new_data (data) decide_result (a 0recoverable testworthy case 1')
3. Use suite to manage case
# TestSuite.py #-*-coding:utf-8-*-import unittest import TestCase def test_InterFace (): # construct the test case in the test set suite = unittest.TestSuite () suite.addTest (TestCase ("test_Case1")) # unittest runner = unittest.TextTestRunner () runner.run (suite) # the value needed to test the test set is # return suite if name = = 'main': # unittest .main (defaultTest='test_InterFace') # execute test runner = unittest.TextTestRunner () runner.run (test_InterFace ())
4. Make statistics on the data of the interface
# TestCensus.py #-*-coding:utf-8-*-import time import TestSuite import send_email import TestCase class Test_Calss (): def census (self): text =''# initialization test start time start_time = time.time () # call suite test set TestSuite.test_InterFace () # end execution time calculation end_time = time.time () Result = TestCase.decide_Count () # Interface test statistics show that total_use_case = u "Total number of execution cases:" + str (result ['num_success'] + result [' num_fail']) +\ u "\ t passed:" + str (result ['num_success']) +\ u "\ t failed:" + str (result [' num_fail']) ]) total_time = u "\ t Total time:" + str (round ((end_time-start_time)) 3) + u 'seconds' text = result ['text'] + total_use_case + total_time print (text) # Test report email send_email.email_file (text) if name = =' main': Test_Calss (). Census () these are all the contents of the article "python's method of testing the interface using unittest" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!