比如有一个存钱的接口,合法参数是所有的正整数,那么我们的自动化用例可以根据这个需求设计自动化用例,合法用例包括: 1, 2, 3, … (可以加入随机正整数)
非法用例包括:0, -1, “dawdadwa”…
这种时候,我们希望自动化能正确的去进行测试,只需要配置哪些是合法的用例,哪些是非法的用例,我们可以尝试如下方式:
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返回:单参数例如: (["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)
执行命令python -m pytest -m finished -v test_save_money.py
0.03s teardown priate/1.py::test_save_money_failure[H] 0.02s teardown priate/1.py::test_save_money_failure[P] 0.02s teardown priate/1.py::test_save_money_failure[G] 0.02s teardown priate/1.py::test_save_money_failure[W] 0.02s teardown priate/1.py::test_save_money_failure[R] 0.02s teardown priate/1.py::test_save_money_failure[N] 0.02s teardown priate/1.py::test_save_money_success[7] 0.02s teardown priate/1.py::test_save_money_failure[n] 0.02s teardown priate/1.py::test_save_money_failure[0] 0.02s teardown priate/1.py::test_save_money_failure[Q] 0.02s teardown priate/1.py::test_save_money_failure[i] 0.02s teardown priate/1.py::test_save_money_failure[d] 0.02s teardown priate/1.py::test_save_money_failure[F] 0.02s teardown priate/1.py::test_save_money_failure[O] 0.02s teardown priate/1.py::test_save_money_failure[L] 0.01s teardown priate/1.py::test_save_money_failure[J] 0.01s teardown priate/1.py::test_save_money_failure[S] 0.01s teardown priate/1.py::test_save_money_success[1] 0.01s teardown priate/1.py::test_save_money_failure[K] 0.01s teardown priate/1.py::test_save_money_failure[T] 0.01s teardown priate/1.py::test_save_money_success[5] 0.01s teardown priate/1.py::test_save_money_failure[X] 0.01s teardown priate/1.py::test_save_money_failure[E] 0.01s teardown priate/1.py::test_save_money_failure[I] 0.01s teardown priate/1.py::test_save_money_failure[-1] 0.01s teardown priate/1.py::test_save_money_failure[U] 0.01s teardown priate/1.py::test_save_money_success[2] 0.01s teardown priate/1.py::test_save_money_failure[D] 0.01s teardown priate/1.py::test_save_money_failure[o] 0.01s teardown priate/1.py::test_save_money_failure[Y] 0.01s teardown priate/1.py::test_save_money_failure[q] 0.01s teardown priate/1.py::test_save_money_failure[M] 0.01s teardown priate/1.py::test_save_money_success[6] 0.01s teardown priate/1.py::test_save_money_failure[g] 0.01s teardown priate/1.py::test_save_money_failure[A] 0.01s teardown priate/1.py::test_save_money_failure[k] 0.01s teardown priate/1.py::test_save_money_failure[t] 0.01s teardown priate/1.py::test_save_money_success[9] 0.01s teardown priate/1.py::test_save_money_failure[p] 0.01s teardown priate/1.py::test_save_money_success[167] 0.01s teardown priate/1.py::test_save_money_failure[m] 0.01s teardown priate/1.py::test_save_money_failure[w] 0.01s teardown priate/1.py::test_save_money_failure[z] 0.01s teardown priate/1.py::test_save_money_failure[_] 0.01s teardown priate/1.py::test_save_money_success[3] 0.01s teardown priate/1.py::test_save_money_failure[c] 0.01s teardown priate/1.py::test_save_money_failure[V] 0.01s teardown priate/1.py::test_save_money_failure[v] 0.01s teardown priate/1.py::test_save_money_failure[s] 0.01s teardown priate/1.py::test_save_money_success[4] 0.01s teardown priate/1.py::test_save_money_failure[b] 0.01s teardown priate/1.py::test_save_money_failure[u] 0.01s teardown priate/1.py::test_save_money_failure[r] 0.01s teardown priate/1.py::test_save_money_failure[B] 0.01s teardown priate/1.py::test_save_money_failure[x] 0.01s teardown priate/1.py::test_save_money_failure[l] 0.01s teardown priate/1.py::test_save_money_failure[Z] 0.01s teardown priate/1.py::test_save_money_failure[C] 0.01s teardown priate/1.py::test_save_money_failure[y] 0.01s teardown priate/1.py::test_save_money_success[8] 0.01s teardown priate/1.py::test_save_money_failure[-] 0.01s teardown priate/1.py::test_save_money_failure[+] 0.01s teardown priate/1.py::test_save_money_failure[h] 0.01s teardown priate/1.py::test_save_money_failure[f] 0.01s teardown priate/1.py::test_save_money_failure[a] 0.01s teardown priate/1.py::test_save_money_failure[j] 0.01s teardown priate/1.py::test_save_money_failure[e] (134 durations < 0.005s hidden. Use -vv to show these durations.) =========================================================================== 67 passed, 1 deselected in 1.40s ===========================================================================
其中的
0.01s teardown priate/1.py::test_save_money_success[167]
就是自己配置的随机函数的值,这样就可以通过自己配置合法参数和非法参数在单个参数情况下自动生成测试用例
test_model = { param_append: { key_valid_param: [*sample_int, random_int], key_invalid_param: [*list(mark), *list(sample_str), minus, 0] } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)