Pytest学习笔记4——测试步骤

  引言

  大家都知道功能测试用例组成的基本要素:用例ID,模块名,测试用例名称,前置条件,测试步骤,预期结果,实际结果等等。前置条件以及测试参数化,前面已经讲过,现在简单讲一下pytest测试步骤的用法。

  案例

  如果想在报告中展示测试步骤,需要使用装饰器@allure.step(),该装饰器写在测试步骤函数上面,测试方法直接调用测试步骤函数即可。具体看代码:

import allure

@allure.step("第一步")
def step_1():
    step_2()
    print("打开浏览器")

@allure.step("第二步")
def step_2():
    step_3()
    print("输入URL")

@allure.step("第三步")
def step_3():
    step_4(username='test',password=123456)
    print("输入账号和密码")
    pass

@allure.step('第四步:登录')
def step_4(username,password):
    print("登录")
    print(username,password)

@allure.step("第五步")
def test_login():
    step_1()

  

  运行结果:

  输入命令:pytest -s --alluredir allure_report

================================================================================= test session starts ==================================================================================
platform win32 -- Python 3.6.5, pytest-5.4.1, py-1.7.0, pluggy-0.13.1
rootdir: C:\Users\Administrator\Desktop\pytest_test_2019\test_pytest
plugins: allure-pytest-2.8.13, celery-4.3.0, cov-2.7.1, forked-1.1.3, html-1.20.0, metadata-1.8.0, rerunfailures-5.0, xdist-1.32.0
collected 1 item

test_step.py 登录
test 123456
输入账号和密码
输入URL
打开浏览器
.

================================================================================== 1 passed in 0.06s ===================================================================================

  输入命令:allure serve allure_report

Report successfully generated to C:\Users\ADMINI~1\AppData\Local\Temp\6826110311791803428\allure-report
Starting web server...
2020-06-05 23:41:51.453:INFO::main: Logging initialized @2015ms to org.eclipse.jetty.util.log.StdErrLog
Server started at <http://192.168.1.105:49560/>. Press <Ctrl+C> to exit

  自动打开浏览器,查看报告:

 

 

  总结

  @allure.step()基本用法就是这样,建议嵌套使用,单独使用的意义并不大。

 另外,对测试开发,自动化测试,全栈测试相关技术感兴趣的朋友,可以加入到群里学习和探索交流,进群方式,扫下方二维码。

posted @ 2020-06-05 23:48  全栈测试开发日记  阅读(657)  评论(0编辑  收藏  举报