Python data-driven examples

Source: Internet
Author: User

First Download Install unittest-data-provider-1.0.1.tar.gz

Example one:

#myclass. Pyclass MyClass (): def sum (self,num_a,num_b): Return Num_a+num_b def sub (self,num_a,num_b): R Eturn Num_a-num_b
#test_myClass. Pyimport unittestfrom unittest_data_provider Import data_providerfrom myClass import Myclassclass Testmyclass (unittest.        TestCase): Sum_provider=lambda: (        (1,1,2), (1,3,4), (+/-), @data_provider (Sum_provider) def test_sum (self,num_a,num_b,res): Print Num_a,num_b,res Test=myclass () self.assertequal (Res,test.sum (Num_a,num_b))

Example two:

#sort. Pyclass sort ():      @staticmethod     def bubble_ Sort (arr):         arr=list (arr)          if len (arr) <=1:             Return arr        for i in range (1,len (arr)):             for j in range (Len (arr) -I.):                if  arr[j] > arr[j+1]:                     arr[j],arr[j+1]=arr[j+1],arr[j]         return arr             @ staticmethod    def quick_sort (arr):         arr=list (arr)          if len (arr) <=1:             return arr        arr_l = []         arr_r = []         arr_m = []        key = arr[0]         for i in arr:             if i<key:                 arr_l.append (i)              elif i>key:                 arr_r.append (i)             else:                 arr_m.append (i)          arr_l = sort.quick_sort (arr_l)          arr_r = sort.quick_sort (Arr_r)         return arr_ L + arr_m + arr_r
#test_sort. pyimport unittestfrom Sort Import sortfrom unittest_data_provider import Data_providerclass testsort ( UnitTest.         TestCase): Quick_sort_provider = Lambda:( ([]), ([7]), ([15,12,36,22,1,7,18]), ((15,12,36,22,1,7,18)),) @data_provider (quick_sort_p Rovider) def test_quick_sort (Self,*arr): #print sorted (arr) self.assertequals (sorted (arr), sort.quick_s ORT (ARR))


This article from "Today's efforts, tomorrow's success!" "Blog, be sure to keep this provenance http://zhzhgo.blog.51cto.com/10497096/1689612

Python data-driven examples

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.