环境配置:
- python
- pytest [pip install pytest]
- allure [https://github.com/allure-framework/allure2/releases/tag/2.10.0]. 把bin配置到path 全局变量中。
- pytest-allure-adaptor [pip install pytest-allure-adaptor]
测试脚本:【这个脚本是转的,应该是春明同学】
#!/usr/bin/env python
# coding=utf-8
import pytest
import allure
@allure.feature('购物车功能') # feature定义功能
class TestShoppingTrolley(object):
@allure.story('加入购物车') # story定义用户场景
def test_add_shopping_trolley(self):
login('刘春明', '密码') # 调用“步骤函数”
with allure.step("浏览商品"): # 将一个测试用例分成几个步骤,将步骤打印到测试报告中,步骤2
allure.attach('商品1', '刘春明') # attach可以打印一些附加信息
allure.attach('商品2', 'liuchunming')
with allure.step("点击商品"): # 将一个测试用例分成几个步骤,将步骤打印到测试报告中,步骤3
pass
with allure.step("校验结果"):
allure.attach('期望结果', '添加购物车成功')
allure.attach('实际结果', '添加购物车失败')
assert 'success' == 'failed'
@allure.story('修改购物车')
def test_edit_shopping_trolley(self):
pass
@pytest.mark.skipif(reason='本次不执行')
@allure.story('删除购物车')
def test_delete_shopping_trolley(self):
pass
@allure.step('用户登录') # 还可以将一个函数作为一个步骤,调用此函数时,报告中输出一个步骤,步骤名字通常是函数名,我把这样的函数叫“步骤函数”
def login(user, pwd):
print(user, pwd)
Allure提供了以下常用注解(未列出部分请访问官网了解),具体用法如下。
- @Epic
往下再分Feature和Story。 - @Feature
敏捷的术语,定义功能模块,往下是Story。 - @Story
定义用户故事。 - @Title
定义用例名称。 - @Description
定义用例描述。
生成测试报告:
- 运行:pytest test_allure.py --alluredir report
- 把生成的xml 转化为html: allure generate report/ -o htmlreport --clean
坑:
- 使用firefox 打开测试报告、使用firefox 打开测试报告、使用firefox 打开测试报告
接下来我们将考虑,如何把这个与jenkins 联系起来。和selenium 联系起来,做UI自动化测试