TestStepsPython 测试软件包
TestSteps 是一个 Python 软件包,通过提供一系列的模块和函数,帮助 Python 开发和测试人员方便快速地编写测试用例,并自动记录日志。
TestSteps 提供的主要功能:
自动记录日志
根据环境变量TESTSUITE_CONFIG_PATH自动装载相应的TestBed描述并初始化测试环境;完全实现测试用例的移植性(不同的环境定义不同的TestBed文件)
支持环境变量TESTSTEP_LOG_PATH 定义日志目录
提供预定义的多个check函数(用于判断pass/fail)
提供多个选项用于测试判断,如:timeout,Exception, retry, xfail;使测试用例简洁清晰易读
提供接口用于函数和选项扩展
安装:
pip install test_steps
简单的例子:
from test_steps import * def test_example() ok("just pass the check and log it") #fail("Just fail the check and log it") ok(3+2 == 5, "pass if expr else fail") #eq("Shanghai", "Beijing", "Shanghai not equal to Beijing") eq(4+5, 9) ne("Shanghai", "Beijing", "Pass, Shanghai not equal to Beijing") #'Shanghai City' contains 'Country', the second parameter could be regex match("Shanghai City", "Country") unmatch("Shanghai City", "Country", "Pass, not contains, regex can be used too")
日志样例:
2015-01-10 20:43:22,787 - INFO - ------------------------------------------------------ 2015-01-10 20:43:22,788 - INFO - Func test_example in file: /Users/Steven004/test/demo.py 2015-01-10 20:43:22,788 - INFO - Check-1: just pass the check and 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, Shanghai not equal to Beijing - PASS: 2015-01-10 20:43:29,792 - ERROR - Check-5: "Shanghai City" =~ "Country" - FAIL: "Shanghai City" =~ "Country"?
带选项的 check 函数:
# Just as match(string1.range(1..4), r'\w\-\w') function check("match(string1.range(1..4), r'\w\-\w')") # Run the code string; pass if it return in 15 seconds, or fail with timeout exception check("num_async.data_sync()", timeout = 15) # repeat option. In 20 seconds, if the expr returns False, re-run it every another second, # until it returns True (which means pass), or time is out (which means fail) check("num_async.get_value() == 500", repeat = 20, xfail = True) # Run code_string in a particular name space, here, to run code string in shanghai object's name space check("cars.averagespeed() > 50 ", globals = shanghai.__dict__) check("1/0", exception=ZeroDivisionError, passdesc='Pass, expected to have the ZeroDivisionError')
支持如下选项:
- timeout: 例如 timeout=30, 如果30秒内调用未返回则自动终止,并fail - repeat: 例如 repeat=20, 如果返回失败,则再次执行,直到成功或达到所设定的次数(本例中为20) - duration: 例如 duration=15, 设定此步骤执行时间15秒,若提前完成则剩余时间休眠 - xfail: 例如 xfail=True, 预期失败,即失败则返回成功,成功则返回失败 - warning: 例如 warning=True, 无论此步骤成功或失败,均返回成功,但如果失败,日志记录一个警告信息 - skip: 例如 skip=True, 不运行此步骤,直接返回成功(用于一个暂时不运行的步骤). - exception: 例如 exception=NameError, 此步骤应有NameError异常,是则返回成功,否则返回失败 - passdesc: 例如 passdesc="the string to log if passed" (如果成功,用passdec定义的串来记录日志,缺省是用调用串) - faildesc: 例如 faildesc="the string to log if failed" (如果失败,用faildec定义的串来记录日志,缺省是用调用串)
check函数中使用的测试操作符:
== : eq != : ne > : gt < : lt >= : ge <= : le =~ : match !~ : unmatch =>: has !> hasnt
高级应用:
请参见原文档。包括:
三步自定义判断函数,享用所有TestSteps特性
为Check添加自定义选项
Testbed 初始化方法(支持.py 和.yaml文件)
日志格式和存储设置
评论