自动化二:框架模式

数据驱动+关键字驱动=混合驱动app

#数据驱动模型
import traceback
#测试数据
with open("D:\\aaa.txt") as fp:
    data = fp.readlines()


#被测试对象
def add(a, b):
    return a+b


def sub(a,b):
    return a-b


def execute_test_step(func_name,value1,value2):
    test_data= "%s(%s,%s)" %(func_name,value1,value2) 
    print("关键字的数据:",test_data)
    actual_result = eval(test_data)
    return actual_result

def get_test_data(test_data_file_path):
    test_data = []
    with open(test_data_file_path) as fp:
        for i in fp.readlines():
            param1,param2,expect_result = i.strip().split("||")
            test_data.append((int(param1),int(param2),int(expect_result)))
    return test_data  

def assert_result(actual_result,expected_result,func_name,value1,value2):
    try:
        assert actual_result == int(expected_result)
        print("测试执行成功,测试数据是%s,%s,%s" %(func_name,value1,value2))
    except AssertionError:

        print("当前测试用例执行失败,测试数据是%s,%s,%s" %(func_name,value1,value2))
        print("指望的结果%s,实际的结果%s" %(expected_result,actual_result))


#测试程序:
for line in data:
    if len(line.strip().split("||"))==4:
        func_name,value1,value2,expected_result = line.strip().split("||")
        actual_result = execute_test_step(func_name,value1,value2)
        assert_result(actual_result,expected_result,func_name,value1,value2)
        print("________________________________________________")
    elif len(line.strip().split("||"))==2:

        func_name,data_file_path = line.strip().split("||")
        test_data = get_test_data(data_file_path)
        for data in test_data:
            actual_result=execute_test_step(func_name,data[0],data[1])
            assert_result(actual_result,data[2],func_name,data[0],data[1])
            print("________________________________________________")
    else:
        print("$$$$$$$$$$$$$$",data)



    
相关文章
相关标签/搜索