Pytest--详解03

allure报告的定制以及参数化

1.定制代码如下:

import allure
import pytest
# 左边定制
@allure.epic("汽车尾气检测项目")
@allure.feature('用户模块')
class Test_api():
    @allure.story("登录接口")
    # 严重程度
    @allure.severity(allure.severity_level.BLOCKER)
    # 链接
    @allure.link(url="www.baidu.com", name='百度')
    @allure.issue("bug链接")
    @allure.testcase("测试用例链接")
    def test_01_api(self,base_url):
        allure.dynamic.title("用例标题,参数化使用")
        allure.dynamic.description("登录接口描述,参数化使用")
        #步骤
        with allure.step("第一步:打开浏览器"):
            print("这是第一步")

        with allure.step("第二步:输入账号密码"):
            print("这是第二步")
        for i in range(3,10):
            with allure.step(f"这是第{i}步"):
                print("这是第三步之后的步骤")
        # 附件
        with open(r"D:/Titen/baogao.png", 'rb') as f:
            value = f.read()
            allure.attach(name="错误截图", body=value, attachment_type=allure.attachment_type.PNG)
        print(f'这是第一个用例地址是{base_url}')

2.参数化:@pytest.mark.parametrize(args_name,args_value)

如:

@pytest.mark.parametrize("name,age", [['zhangsan', '12'], ['lisi', '13'], ['wangwu', '17']])
def test_03_api(self, name, age):
    print(f"{name}已经满{age}")

结合yaml:

安装:

pip install pyyaml

 

由两种数据格式组成: 1.map对象:键:(空格)值。

                    name: 张三

           2.list列表:用一组-开头。同一个级别的-是同一个list     

                - name1: 张三

                - name2: 李四

读取yaml:(这里打代码市场Loader和FullLoader写成小写而出错)

import yaml

def get_yaml(path):
    with open(path,encoding="utf-8") as f:
        value = yaml.load(f,Loader=yaml.FullLoader)
        print(value)
        return value

yaml文件:

-
  name: 接口1
  request:
    method: get
    url: https://1111
    data:
      grant_type: client
      appid: wx
      secret: 307c
    headers: None
  validate: None

-
  name: 接口2
  request:
    method: get
    url: https://1111
    data:
      grant_type: client
      appid: wx
      secret: 307c
    headers: None
  validate: None

test_api:

@pytest.mark.parametrize("args", get_yaml('./testcases/get_token.yaml'))
    def test_04_api(self, args):
        # print(args)
        datas = args['request']['data']
        print(datas)
        urls = args['request']['url']
        print(urls)
        res = requests.get(url=urls,params=datas)
        print(res.json())

3.allure文件被局域网访问

allure open ./reports

 

  每篇一句:

  傻瓜

 

posted on 2021-12-04 16:06  Titen  阅读(46)  评论(0编辑  收藏  举报

导航