Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3648536
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2021-11-17 17:24:44

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_modelkeyparam_append的合法参数单个参数显示如:[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]

    # 比如test_modelkeyparam_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返回:单参数例如:   (["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 外汇跟单gendan5.com中的返回后为["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)

阅读(892) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~