前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >接口自动化测试之pytest用例管理框架3

接口自动化测试之pytest用例管理框架3

作者头像
用户6367961
发布2022-11-21 12:02:00
5640
发布2022-11-21 12:02:00
举报
文章被收录于专栏:自学测试之道

场景:

前端自动化测试经常需要附加图片或html,在适当的地方,适当的时机截图解决:

@allure.attach显示许多不同类型的提供的附件,可以补充测试,步骤或测试结果。

步骤:

在测试报告里附加网页:

allure.attach(body(内容), name, attachment_type, extension):

allure.attach('<head></head><body>首页</body>', '这是错误页的结果信息', allure.attachment_type.HTML)

在测试报告里附加图片:

allure.attach.file(source, name, attachment_type, extension):

allure.attach.file("./result/b.png",attachment_type=allure.attachment_type.PNG)

pytest测试实战关键点说明

pytestfixture

Fixture是在测试函数运行前后,由pytest执行的外壳函数,代码可以定制,满足多变的测试需求,功能包括:

  • 定义传入测试中的数据集
  • 配置测试前系统的初始状态
  • 为批量测试提供数据源等

Fixture是pytest用于将测试前后进行预备,清理工作的代码分离出核心测试逻辑的一种机制

pytestfixture用法

Fixture是为了测试用例的执行,初始化一些数据和方法

  • 类似setUp, tearDown 功能,但比 setUp, tearDown更灵活
  • 直接通过函数名字调用或使用装饰器@pytest.mark.usefixtures('test1')
  • 允许使用多个Fixture
  • 使用autouse自动应用,如果要返回值,需要传fixture函数名
  • 作用域(session>module>class>function)

- -setup-show 回溯fixture的执行过程

自动化应用场景1:

场景:

  • 测试用例执行时,有的用例需要登陆才能执行,有些用例不需要登陆。setup和teardown无法满足。fixture可以。默认scope(范围)function

步骤:

1.导入pytest

2.在登陆的函数上面加@pytest.fixture()

3.在要使用的测试方法中传入(登陆函数名称),就先登陆

4.不传入的就不登陆直接执行测试方法。

conftest.py

场景:

  • 你与其他测试工程师合作一起开发时,公共的模块要在不同文件中,要在大家都访问到的地方。

解决:

  • 使用conftest.py这个文件进行数据共享、并且他可以放在不同位置起着不同的范围共享作用。

前提:

  • conftest.py文件名是不能换的,放在项目下是全局的数据共享的地方,全局的配置和前期工作都可以写在这里,放在某个包下,就是这个包数据共享的地方。

执行:

  • 系统执行到参数login时先从本文件中查找是否有这个名字的fixture方法,之后在conftest.py中找是否有。

步骤:

  • 将登陆模块带@pytest.fixture写在conftest.py

conftest.py用法

conftest.py配置需要注意:

  • conftest.py 文件名是不能换的
  • conftest.py 与运行的用例要在同一个package下,并且有_ init_ .py文件
  • 不需要import导入conftest.py, pytest用 例会自动查找
  • 所有同目录测试文件运行前都会执行conftest.py文件
  • 全局的配置和前期工作都可以写在这里,放在某个包下,就是这个包数据共享的地方。

yaml的应用

代码语言:javascript
复制
test_demo.py:
#coding=utf-8
import pytest
import yaml

def get_datas():
    with open("./data/data.yml") as f:
        datas = yaml.safe_load(f)
    return datas

class TestCal:
    @pytest.mark.parametrize('a,b,expect', get_datas()['add_int']['datas'])
    def test_add_int(self, calculate, a, b, expect):
        assert expect == calculate.add(a, b)

    @pytest.mark.parametrize('a,b,expect', get_datas()['add_float']['datas'], ids=get_datas()['add_float']['ids'])
    def test_add_float(self, calculate, a, b, expect):
        assert expect == round(calculate.add(a, b), 2)

    def test_div(self, calculate):
        with pytest.raises(ZeroDivisionError):
            calculate.div(1, 0)
代码语言:javascript
复制
data.yml:
add_int:
  datas:
    - [10, 10, 20]
    - [1, 1, 2]

add_float:
  datas:
    - [0.1, 0.1, 0.2]
    -[0.1, 0.2, 0.3]
  ids:
    - 'float1'
    - 'float2'
代码语言:javascript
复制
conftest.py:
#coding=utf-8

import pytest
from calculator import Calculator

@pytest.fixture()
def calculate():
    print("开始计算")
    cal = Calculator()
    yield cal
    print("结束计算")
代码语言:javascript
复制
calculator.py:
#coding:utf-8
class Calculator:
    def add(self, a, b):
        return a + b

    def div(self, a, b):
        return a / b
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-07-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 自学测试之道 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档