compute.py #该文件用来存被测函数

#encoding=UTF-8
import random
import re
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 mul(a,b,c):
    d=int(a)
    e=int(b)
    f=int(c)
    return d*e*f

def 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):#看预期结果是否是个正则表达式,看里面是否含有.*?斜杠中的任意一个
#.匹配除换行符之外的任意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,3
sub,2,1,1
mul,1,2,3,6
abs_value,-1,1
random_value,\d+
#--------------------------------------------------------------------------------------------------
test.py #要运行的程序
#encoding=UTF-8
from compute import *
test_case_num = 0
success_test_case =0
faile_test_case = 0
with 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 +=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

yzc388亚洲城,该例子由3局地构成,实现了从txt文件里读数据,每一个主要字映射到一个函数

重中之重字驱动小例子_example1,关键字_example1

关键字驱动小例子_example1,关键字驱动小例子_example2。该例子由叁部分构成,完毕了从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
为数据文件,可放在Computer某盘根目录下也可径直放在测试程序所在文书夹里

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

 

该例子由三片段构成,达成了从txt文件里读数据,每一种珍视字映射到一个函数
compute.py 为被测函数…

重大字驱动小例子_example2,关键字_example2

compute.py #该文件用来存被测函数

#encoding=UTF-8
import random
import re
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 mul(a,b,c):
    d=int(a)
    e=int(b)
    f=int(c)
    return d*e*f

def 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):#看预期结果是否是个正则表达式,看里面是否含有.*?斜杠中的任意一个
#.匹配除换行符之外的任意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,3
sub,2,1,1
mul,1,2,3,6
abs_value,-1,1
random_value,\d+
#--------------------------------------------------------------------------------------------------
test.py #要运行的程序
#encoding=UTF-8
from compute import *
test_case_num = 0
success_test_case =0
faile_test_case = 0
with 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 +=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

 

compute.py # 该文件用来存被测函数 # encoding=UTF-8 import random
import re def add(a,b): c = int(a) d = int(b) prin…

 

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
网站地图xml地图