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

來源:互聯網
上載者:User

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

該例子由3部分組成,實現了從txt檔案裡讀資料,每個關鍵字映射到一個函數

compute.py 為被測函數

#encoding=UTF-8def 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 assert_value(a,b):    if a==b:        return True    else:        return False

data.txt 為資料檔案,可放在電腦某盤根目錄下也可直接放在測試程式所在檔案夾裡

add,1,2,3sub,2,1,3

test.py 為要啟動並執行程式

#encoding=UTF-8from compute import *test_case_num = 0success_test_case =0faile_test_case = 0# with open("e:\\data.txt") as f:with open("data.txt") as f:    for line in f:        test_case_num += 1        func_name = line.split(",")[0]        value1  = line.split(",")[1]        value2  = line.split(",")[2]        expect_value = line.split(",")[3]        s= func_name+"("+value1+","+value2+")"        print s        actual_result=eval(s)        if assert_value(actual_result,int(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

 運行結果:

add(1,2)3sub(2,1)1total 2 test cases runnedtotal 1 succeeded test cases runnedtotal 1 failed test cases runned

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.