欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

关键字驱动小例子_example1

程序员文章站 2022-04-14 12:53:48
该例子由3部分组成,实现了从txt文件里读数据,每个关键字映射到一个函数 compute.py 为被测函数 data.txt 为数据文件,可放在电脑某盘根目录下也可直接放在测试程序所在文件夹里 test.py 为要运行的程序 运行结果: ......

该例子由3部分组成,实现了从txt文件里读数据,每个关键字映射到一个函数

compute.py 为被测函数

#encoding=UTF-8
def add(a,b):
    c=int(a)
    d=int(b)
    print c+d
    return c+d


def sub(a,b):
    c=int(a)
    d=int(b)
    print c-d
    return c-d

def assert_value(a,b):
    if a==b:
        return True
    else:
        return False

data.txt 为数据文件,可放在电脑某盘根目录下也可直接放在测试程序所在文件夹里

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

test.py 为要运行的程序

#encoding=UTF-8
from compute import *
test_case_num = 0
success_test_case =0
faile_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 +=1

print "total %s test cases runned" % test_case_num
print "total %s succeeded test cases runned" % success_test_case
print "total %s failed test cases runned" % faile_test_case

 运行结果:

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