0%

python自动化测试简易封装

背景

工作中遇到一些类似责任链试的全链路测试场景,总是得临时写一些测试脚本,刚好觉得这种场景和「junit」的代码编写类似,于是考虑用「python」开发支持下

细节说明

主体代码

代码量很小,直接贴出来:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import sys
import traceback
import time
import inspect
from functools import wraps

AUTO_TEST_PREFIX = "autotest"
SPLIT = "_"


def test(message=""):
def decorate(func):
@wraps(func)
def wrapper(*args, **kwargs):
print("-" * 80)
start = time.time()
print("start testing, api: %s" % func.__name__)
print("detail message: ", message)
print("=" * 20 + "body start" + "=" * 20)
res = func(*args, **kwargs)
print("=" * 20 + "body end" + "=" * 20)
print("run cost: %fs" % (time.time() - start))
return res

return wrapper

return decorate


def start(name):
mod = sys.modules[name]
if mod:
def func_filter(func):
return SPLIT in func.__name__ and func.__name__.startswith(AUTO_TEST_PREFIX)

def func_sort(func):
pairs = func.__name__.split(SPLIT)
if len(pairs) == 2:
return int(pairs[1])
return 0

funcs = inspect.getmembers(mod, inspect.isfunction)
funcs = [func[1] for func in funcs if func_filter(func[1])]
for func in sorted(funcs, key=func_sort):
try:
res = func()
if type(res) is bool:
if not res:
print("error: api %s run failed!" % func.__name__)
break
else:
print("info: api %s run success!" % func.__name__)
elif not res:
print("warn: api %s with no return, you may need to check." % func.__name__)
else:
print("info: api %s run with result:%s" % (func.__name__, str(res)))
except Exception:
traceback.print_exc()
break
finally:
print("\n\n")

其实就是利用装饰器,黑盒处理了自动化测试的流程

用法

引入两个主题代码的两个「func」,「test」类似「junit」的注释,「start」为执行入口
此外可以简单对「func」添加下接口说明,便于理解接口功能,在自动化测试时方便快速定位

from auto_test import test
from auto_test import start


@test(message="login")
def autotest_0():
    print("process about login")

@test(message="create instance")
def autotest_1():
    print("process about create")
    return True

@test(message="config")
def autotest_2():
    print("process about config")
    return False

@test(message="destroy instance")
def autotest_3():
    print("process about destroy")


start(__name__)

查看执行结果:

--------------------------------------------------------------------------------
start testing, api: autotest_0
detail message:  login
====================body start====================
process about login
====================body   end====================
run cost: 0.000025s
warn: api autotest_0 with no return, you may need to check.

--------------------------------------------------------------------------------
start testing, api: autotest_1
detail message:  create instance
====================body start====================
process about create
====================body   end====================
run cost: 0.000022s
info: api autotest_1 run success!

--------------------------------------------------------------------------------
start testing, api: autotest_2
detail message:  config
====================body start====================
process about config
====================body   end====================
run cost: 0.000020s
error: api autotest_2 run failed!

按照接口顺序额打印出了各个接口的信息,执行耗时以及接口的输出
整个测试链路仅执行prefix为「autotest」的「func」,执行顺序按照subfix的编号,当且仅当该「func」执行不抛异常且执行结果不为「false」时才会继续流转
再针对具体的工作场景,沉淀一些「tools」,「utils」包,后面就交给他人填充「case」就好了,干净整洁

  • 本文作者: Xsank
  • 本文链接: xsank.com/posts/5f957c93/
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!