关于python:自动化测试生成用例一之单个参数

38次阅读

共计 2854 个字符,预计需要花费 8 分钟才能阅读完成。

import random
import string
from copy import deepcopy
from types import FunctionType
import pytest
from log import logger
deposit = 0
key_valid_param = “key_valid_param”
key_invalid_param = “invalid_para”
param_append = “param_append”
success = “success”
failure = “failure”
sample_str = string.ascii_letters
sample_int = list(range(1, 10))
minus = -1
mark = “-+_”
def random_int():

return random.randint(100, 200)

def function_get_value(args):

new_args = []
for arg in args:
    new_arg = list(map(lambda x: x() if isinstance(x, FunctionType) else x, arg))
    new_args.append(new_arg)
return new_args

def generate_case(*args, module, generate):

# args 指的是 module 中应用哪些参数作为验证的 key
cases = []
args = list(args)
row = []
# 上面两个 for 循环是为了构建要测试的 cases,依据传入的 args 和 generate, 在 module 中取出非法或者非法参数,
# 比方 test_model 中 key 为 param_append 的非法参数单个参数显示如:[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]
# 比方 test_model 中 key 为 param_append 的非法参数单个参数显示如:[['-'], ['+'], ['_'], ['a'], ['b'], ['c'], ['d'], ['e'],
# ['f'], ['g'], ['h'], ['i'], ['j'], ['k'], ['l'], ['m'], ['n'], ['o'], ['p'], ['q'], ['r'], ['s'], ['t'], ['u'],
# ['v'], ['w'], ['x'], ['y'], ['z'], ['A'], ['B'], ['C'], ['D'], ['E'], ['F'], ['G'], ['H'], ['I'], ['J'], ['K'],
# ['L'], ['M'], ['N'], ['O'], ['P'], ['Q'], ['R'], ['S'], ['T'], ['U'], ['V'], ['W'], ['X'], ['Y'], ['Z'], [-1]]
for arg_index in range(len(args)):
    row.insert(arg_index, module[args[arg_index]][key_valid_param][0])
for column_index in range(len(args)):
    if generate == success:
        param = key_valid_param
    else:
        param = key_invalid_param
    row_values = module[args[column_index]][param]
    if not len(row_values):
        continue
    for row_value in row_values:
        row_copy = deepcopy(row)
        row_copy[column_index] = row_value
        cases.append(row_copy)
# 将 cases 中的函数进行应用
cases = function_get_value(cases)
logger.info("cases:{}".format(cases))
# 将 key 和 cases 返回:[Skrill 下载](https://www.gendan5.com/wallet/Skrill.html)单参数例如:(["param_append"], [['0'], ['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9']])
return args, cases

def save_money(money):

global deposit
if isinstance(money, int) and money > 0:
    deposit += money
    return True, deposit
return False, deposit

def test_save_money(money, except_success=True):

old_deposit = deposit
is_success, current_deposit = save_money(money)
if except_success:
    assert is_success and current_deposit == money + old_deposit, "save money has trouble"
else:
    assert not is_success and current_deposit == old_deposit, "save money except failure, but has some trouble"

test_model = {

param_append: {key_valid_param: [*sample_int, random_int],
    key_invalid_param: [*list(mark), *list(sample_str), minus, 0]
}

}
@pytest.mark.finished
@pytest.mark.dev
@pytest.mark.parametrize(*generate_case(param_append, module=test_model, generate=success))

parametrize 中的返回后为[“param_append”], [[‘0’], [‘1’], [‘2’], [‘3’], [‘4’], [‘5’], [‘6’], [‘7’], [‘8’], [‘9’]],

那么上面的传参就应是 [“param_append”] 中的 param_append

def test_save_money_success(param_append):

test_save_money(param_append, True)

@pytest.mark.finished
@pytest.mark.dev
@pytest.mark.parametrize(*generate_case(param_append, module=test_model, generate=failure))
def test_save_money_failure(param_append):

test_save_money(param_append, False)

正文完
 0