首先下载安装unittest-data-provider-1.0.1.tar.gz
示例一:
#myclass.py class myClass(): def sum(self,num_a,num_b): return num_a+num_b def sub(self,num_a,num_b): return num_a-num_b
#test_myClass.py import unittest from unittest_data_provider import data_provider from myclass import myClass class TestMyClass(unittest.TestCase): sum_provider=lambda:( (1,1,2), (1,2,3), (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))
实例二:
#sort.py class 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.py import unittest from sort import Sort from unittest_data_provider import data_provider class 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_provider) def test_quick_sort(self,*arr): #print sorted(arr) self.assertEquals(sorted(arr),Sort.quick_sort(arr))
时间: 2024-10-25 21:16:22