關鍵字驅動小例子_example2,關鍵字_example2

來源:互聯網
上載者:User

關鍵字驅動小例子_example2,關鍵字_example2

compute.py #該檔案用來存被測函數#encoding=UTF-8import randomimport redef add(a,b):    c=int(a)    d=int(b)    print c+d    return c+ddef sub(a,b):    c=int(a)    d=int(b)    print c-d    return c-ddef mul(a,b,c):    d=int(a)    e=int(b)    f=int(c)    return d*e*fdef abs_value(a):    return abs(int(a))def random_value():    return random.randint(1,1000)def assert_value(actual_value,expected_value):    expected_value = str(expected_value).strip()    actual_value = str(actual_value)    if re.search(r"\\|\*|\.|\?",expected_value):#看預期結果是否是個Regex,看裡面是否含有.*?斜杠中的任意一個#.匹配除分行符號之外的任意1個字元  .不能匹配斷行符號;#\d 匹配中任意一個數字,範圍為[0-9]#*(星號) 匹配前一個字元0次1次或多次#?匹配前一個字元0次或1次#豎線表示或,都表示第1個位置        if re.match(expected_value,actual_value):            return True        else:            return False    if actual_value==expected_value:        return True    else:        return False#-------------------------------------------------------------------------------------------------data.txt #將該txt檔案存於電腦E盤根目錄下add,1,2,3sub,2,1,1mul,1,2,3,6abs_value,-1,1random_value,\d+#--------------------------------------------------------------------------------------------------test.py #要啟動並執行程式#encoding=UTF-8from compute import *test_case_num = 0success_test_case =0faile_test_case = 0with open("e:\\data2.txt") as f:    for line in f:        test_case_num+=1        func_name = line.split(",")[0]        value = ",".join(line.split(",")[1:-1])        expect_value = line.split(",")[-1]        s= func_name+"("+value+")"        actual_result=eval(s)        if assert_value(actual_result,expect_value):            success_test_case+=1        else:            faile_test_case +=1print "total %s test cases runned" % test_case_numprint "total %s succeeded test cases runned" % success_test_caseprint "total %s failed test cases runned" % faile_test_case

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.