How Python uses the UnitTest test interface _python

Source: Internet
Author: User
This article mainly for you in detail how Python uses the UnitTest test interface, with a certain reference value, interested in small partners can refer to

The example of this article for everyone to share the Python use UnitTest test interface specific code, for your reference, the specific content is as follows

1. First test the interface using Python's requests

# testinface.py Import Requests,json url = visit.get_test_url () News_url = url+ ' news.info ' headers = Basetoken.basetoken_d Atas () [' Headers '] def new_data (data):      r = Requests.post (news_url,data=data,headers=headers)   cnn = Json.loads (R.text)   Return CNN

2. Use the UnitTest to invoke the interface, and to count the results of the interface test

# testcase.py #-*-coding:utf-8-*-import unittest Import Testinface # Statistics on executed case results #------------------------------ ---------------------------------------------------------------------------------------Text = "" num_success = 0 num   _fail = 0 # test passed def decide_success (joggle): global num_success num_success + = 1 print_out (joggle + ": interface test via \ n") Return num_success # test does not pass def decide_fail (TXT, joggle): global num_fail Num_fail + = 1 print_out (joggle + ": interface test not  VIA \ n Error message: "+ txt +" \ n ") return num_fail # message content Write & client output def print_out (message): Global text text + =" \ n "+ Message return text # return value judgment 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_fai L ': num_fail, ' text ': TexT} return Data #------------------------------------------------------------------------------------------------- -------------------# defines UnitTest class Mytestcase (unittest. TestCase): # Initialize work def setUp (self): Pass # exit cleanup work def tearDown (self): Pass def test_case1 (self): I D = data = {' id ': id} a = testinface.new_data (data) Decide_result (a,0, ' test_case1 ')

3. Manage case with Suite

# testsuite.py #-*-coding:utf-8-*-import unittest import TestCase   def test_interface ():     # construct test set   suite = UN Ittest. TestSuite ()   suite.addtest (TestCase ("Test_case1") # UnitTest the test case runner     = UnitTest. Texttestrunner ()   Runner.run (Suite)   # Testing a test set requires return value   # return Suite  If __name__ = = ' __main__ ':     # Unittest.main (defaulttest= ' test_interface ')   # Execute test   runner = UnitTest. Texttestrunner ()   Runner.run (Test_interface ())

4. 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 description     total_use_case = u "Total number of execution cases:" + str (result[' num_success ') + result[' Num_fail ']) + \              U "\ T pass number:" + str ( result[' num_success ') + \              U "\ t does not pass number:" + str (result[' num_fail '])     total_time = u "\ t total time:" + str (round (end_ Time-start_time), 3) + U ' s '     text = result[' text '] + total_use_case + total_time     print (text)      # occurrence test report message C18/>send_email.email_file (text)  if __name__ = = ' __main__ ':   test_calss (). Census ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.