PyTest测试框架

双11活动

学院介绍

image

知识点

虚拟环境

pytest介绍

fixture介绍

插件体系

常用插件

  • pytest-ordering
  • pytest-xdist
  • pytest-allure
  • pytest-html

接口测试框架

https://requests.readthedocs.io/en/master/

import requests

def test_get():
    r=requests.get('https://ceshiren.com/search.json?q=%E5%A4%B4%E6%9D%A1')
    print(r.status_code)
    assert r.status_code == 200
    assert '今日头条' in r.text

企业微信接口测试实战

企业微信基本参数

https://work.weixin.qq.com/api/doc/90000/90135/92117

{
    'corpid': 'wwd6da61649bd66fea',
    'corpsecret': 'heLiPlmyblHRiKAgGWZky7MMvyld3d3QMUl5ra7NBZU'
}

常见问题

  • 需要依赖token
  • 测试用例之间需要依赖
  • 数据需要传递与复用
  • 数据的清理

演练代码

https://github.com/ceshiren/HogwartsHttp1104

作业

fork本项目代码,完善测试用例中的todo部分,并提交代码并把自己的github作业地址贴到pytest测试框架练习课程贴的回复里,答案优秀的同学可获得礼品奖励。

关键的todo

  • 测试用例之间完全解除依赖
  • 保证测试数据可以重复运行
  • 断言逻辑更完备
  • 出一份带有所有接口响应的allure2报告

参考资料

https://docs.pytest.org/en/stable/index.html

https://docs.pytest.org/en/stable/contents.html

https://github.com/ftobia/pytest-ordering

https://docs.qameta.io/allure/

我想使用@fixture来做参数化,@fixture装饰的函数传入文件名后,函数返回一个字典
@fixture
def setting(filename)
return param

def test_case(setting(filename))
#执行测试

老师,请问生成allure报告的那俩命令是啥呀?

pip install allure-pytest
pytest --alluredir=allure-results tests/
allure serve allure-results/

你这样用不太合适。参考官方的这个例子

返回一个处理过程函数,而不是具体的返回值。比如你的读取文件并返回内容的过程,就可以封装到类似于_make_customer_record的内部函数里。你传参的时候,就可以直接传入fixture和参数化的文件名即可。

@pytest.fixture
def make_customer_record():
    def _make_customer_record(name):
        return {"name": name, "orders": []}

    return _make_customer_record


def test_customer_records(make_customer_record):
    customer_1 = make_customer_record("Lisa")
    customer_2 = make_customer_record("Mike")
    customer_3 = make_customer_record("Meredith")

或者这种模式。使用两个fixture中转

import pytest


class App:
    def __init__(self, smtp_connection):
        self.smtp_connection = smtp_connection


@pytest.fixture(scope="module")
def app(smtp_connection):
    return App(smtp_connection)


def test_smtp_connection_exists(app):
    assert app.smtp_connection

你的情况我猜测是要做一个数据驱动的框架,其实连fixture都用不到的。直接参数化就可以解决了。

老师,我装了pytest-allure的包,也生成了allure的report文件夹,但是在命令行执行【allure serve allure-results/】时提示:【bash: allure: command not found】,请问我这个情况是哪里有问题呀?

@seveniruby 老师,pretty函数里的那句可以发一下吗?刚刚视频里没跟上,谢谢~

你要下载allure安装, 然后在Windows环境变量path添加路径, 比如C:\allure-2.13.6\bin,

1 个赞

感谢指点,是这个问题,电脑安装allure后可以成功生成报告,感谢