關鍵字驅動測試框架搭建(1)

一、小練習:定義三個方法(加法、減法、斷言),經過使用關鍵字驅動測試這個三個方法
compute.pyhtml

#encoding=utf-8

def add(a,b):
    print a + b
    return a + b

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

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

testdata.txt文件
add,1,2,3
sub,2,1,1正則表達式

test_compute.pydom

#encoding=utf-8
from compute import *

test_case_num = 0
success_case_num = 0
faile_case_num = 0
with open("D:\\PythonProject\\MyTest\\testdata.txt") as fp:
    for line in fp:
        test_case_num += 1
        action,data1,data2,expect_result = line.split(',')
        expect_result = eval(expect_result.strip())
        s = action + "(" + data1 + "," + data2 + ")"
        actual_result = eval(s)
        if assert_value(actual_result,expect_result):
            success_case_num += 1
        else:
            faile_case_num += 1

print "total %s test cases ran " % test_case_num
print "total %s succeeded test cases ran " % success_case_num
print "total %s failed test cases ran " % faile_case_num

上面的函數都是兩個參數,若是函數的參數不一樣,有的一個,有的三個,有的沒有,則須要優化拼接eval表達式,如:函數

compute.py測試

 

#encoding=utf-8
import random
import re
#兩個參數
def add(a,b):
    print a + b
    return a + b
#兩個參數
def sub(a,b):
    print a - b
    return a - b
#三個參數
def mul(a,b,c):
    print a*b*c
    return a*b*c
#一個參數
def abs_value(a):
    print abs(a)
    return abs(a)
#無參數,返回值是一個隨機,沒法使用精確值斷言,因此使用正則來斷言
def random_value():
    print random.randint(1,1000)
    return random.randint(1,1000)

def assert_value(actual_result,expected_result):
    #將真實結果、指望結果強制轉換爲字符串類型
    actual_result = str(actual_result)
    expected_result = str(expected_result)
    #判斷指望結果是否包含正則表達式
    if re.search(r"\\|\*|\?",expected_result):
        #若是指望結果是正則表達式,則使用指望結果對真實結果進行匹配
        if re.match(expected_result,actual_result):
            return True
        else:
            return False

    #若是指望結果不是正則表達式,則直接斷言是否相等
    if actual_result == expected_result:
        return True
    else:
        return False

testdata.txt
add,1,2,3
sub,2,1,1
mul,1,2,3,6
abs_value,-1,1
random_value,\d+優化

測試數據的第一行的第一個爲函數名,最後一個爲斷言值,因此中間的均爲參數,須要對切割開的數據進行判斷長度,再取出切片(去頭去尾,即只留參數),最後拼接成一個函數表達式。如:mul(1,2,3)
test_comput.pyspa

#encoding=utf-8
from compute import *

test_case_num = 0
success_case_num = 0
faile_case_num = 0
with open("D:\\PythonProject\\MyTest\\testdata.txt") as fp:
    for line in fp:
        test_case_num += 1
        #切割出函數名
        func_name = line.split(",")[0].strip()
        #切割出參數再拼接成一個字符串
        values = ",".join(line.split(",")[1:-1]).strip()
        #切割出斷言值
        expected_result = line.split(",")[-1].strip()
        #拼接成執行函數的表達式
        s = func_name + "(" + values + ")"
        #使用eval執行表達式,並將結果保存到actual_result
        actual_result = eval(s)
        #進行斷言
        if assert_value(actual_result,expected_result):
            success_case_num += 1
        else:
            faile_case_num += 1

print "total %s test cases ran " % test_case_num
print "total %s succeeded test cases ran " % success_case_num
print "total %s failed test cases ran " % faile_case_num

#生成html測試報告
content =Template("""
<html>
<head><title>光榮之路測試報告</title>
<meta charset="utf-8" />
</head>
<table border="6">
<caption>測試報告</caption>
<tr>
  <td>運行的總用例數</td>
  <td>成功運行的總用例數</td>
  <td>失敗運行的總用例數</td>
</tr>
<tr>
  <td>$num1</td>
  <td>$num2</td>
  <td>$num3</td>
</tr>
</table>
</html>
""")

file_content = content.substitute(num1=test_case_num,num2=success_test_case,num3=faile_test_case
)
with open("result.html","w") as f:
    f.write(file_content)
相關文章
相關標籤/搜索