TestSteps是一个Pytho软件包,通过提供一系列的模块和函数,帮助Pytho开发和测试人员方便快速地编写测试用例,并自动记录日志。
TestSteps提供的主要功能:
自动记录日志
根据环境变量TESTSUITE_CONFIG_PATH自动装载相应的TestBed描述并初始化测试环境;完全实现测试用例的移植性(不同的环境定义不同的TestBed文件)
支持环境变量TESTSTEP_LOG_PATH定义日志目录
提供预定义的多个check函数(用于判断pass/fail)
提供多个选项用于测试判断,如:timeout,Exceptio,retry,xfail;使测试用例简洁清晰易读
提供接口用于函数和选项扩展
安装:
pip istall test_steps简单的例子:
from test_steps import *def test_example() ok("just pass the check ad log it") #fail("Just fail the check ad log it") ok(3+2 == 5, "pass if expr else fail") #eq("Shaghai", "Beijig", "Shaghai ot equal to Beijig") eq(4+5, 9) e("Shaghai", "Beijig", "Pass, Shaghai ot equal to Beijig") #'Shaghai City' cotais 'Coutry', the secod parameter could be regex match("Shaghai City", "Coutry") umatch("Shaghai City", "Coutry", "Pass, ot cotais, regex ca be used too")日志样例:
2015-01-10 20:43:22,787 - INFO - ------------------------------------------------------2015-01-10 20:43:22,788 - INFO - Fuc test_example i file: /Users/Steve004/test/demo.py2015-01-10 20:43:22,788 - INFO - Check-1: just pass the check ad log it - PASS:2015-01-10 20:43:26,789 - INFO - Check-2: pass if expr else fail - PASS:2015-01-10 20:43:26,789 - INFO - Check-3: 9 == 9 - PASS:2015-01-10 20:43:26,789 - INFO - Check-4: Pass, Shaghai ot equal to Beijig - PASS:2015-01-10 20:43:29,792 - ERROR - Check-5: "Shaghai City" =~ "Coutry" - FAIL: "Shaghai City" =~ "Coutry"?带选项的check函数:
# Just as match(strig1.rage(1..4), r'\w\-\w') fuctiocheck("match(strig1.rage(1..4), r'\w\-\w')")# Ru the code strig; pass if it retur i 15 secods, or fail with timeout exceptiocheck("um_asyc.data_syc()", timeout = 15)# repeat optio. I 20 secods, if the expr returs False, re-ru it every aother secod,# util it returs True (which meas pass), or time is out (which meas fail)check("um_asyc.get_value() == 500", repeat = 20, xfail = True)# Ru code_strig i a particular ame space, here, to ru code strig i shaghai object's ame spacecheck("cars.averagespeed() > 50 ", globals = shaghai.__dict__)check("1/0", exceptio=ZeroDivisioError, passdesc='Pass, expected to have the ZeroDivisioError')支持如下选项:
- timeout: 例如 timeout=30, 如果30秒内调用未返回则自动终止,并fail- repeat: 例如 repeat=20, 如果返回失败,则再次执行,直到成功或达到所设定的次数(本例中为20)- duratio: 例如 duratio=15, 设定此步骤执行时间15秒,若提前完成则剩余时间休眠- xfail: 例如 xfail=True, 预期失败,即失败则返回成功,成功则返回失败- warig: 例如 warig=True, 无论此步骤成功或失败,均返回成功,但如果失败,日志记录一个警告信息- skip: 例如 skip=True, 不运行此步骤,直接返回成功(用于一个暂时不运行的步骤).- exceptio: 例如 exceptio=NameError, 此步骤应有NameError异常,是则返回成功,否则返回失败- passdesc: 例如 passdesc="the strig to log if passed" (如果成功,用passdec定义的串来记录日志,缺省是用调用串)- faildesc: 例如 faildesc="the strig to log if failed" (如果失败,用faildec定义的串来记录日志,缺省是用调用串)check函数中使用的测试操作符:
== : eq != : e > : gt < : lt >= : ge <= : le=~ : match !~ : umatch =>: has !> hast高级应用:
请参见原文档。包括:
三步自定义判断函数,享用所有TestSteps特性
为Check添加自定义选项
Testbed初始化方法(支持.py和.yaml文件)
日志格式和存储设置
评论