关键字驱动小例子_example1

该例子由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

 

转载于:https://www.cnblogs.com/xumian/p/8516216.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值