使用: 注册应用 import os from flask_login import LoginManager, current_user login_manager = LoginManager()...login_manager.login_view = 'users.login' # 未登录作的跳转视图 login_manager.session_protection = 'strong'...自定义以请求信息头部或者传递的参数作为键值 这种情况下,使用 request_loader 回调,与 user_loader 回调原理一样,但是它不是接受user_id @login_manager.request_loader...def load_user_from_request_by_args(request): # 使用 api 参数,或者 post 携带的键值 union_id = request.args.get...() return user @login_manager.request_loader def load_user_from_request_by_headers(request): # 使用
-m标记用例 使用mark标记测试用例 标记测试用例步骤 pytest.ini文件注册标签 标签贴到指定的测试用例或者类 @pytest.mark.标签名称 pytest -m “标签名称”(加上双引号...) 标记测试用例方法 1.类标记 方法一:使用 @pytest.mark.名称 进行标记 @pytest.mark.error class TestLogin(unittest.TestCase)...(self): self.assertEqual(1,1) def test_login_2(self): self.assertEqual(1,2) 方法二:使用类属性...pytestmark = [pytest.mark.名称,pytest.mark.名称] class TestLogin(unittest.TestCase): pytestmark = [pytest.mark.login...): self.assertEqual(1,2) 2.方法标记 使用标签 @pytest.mark.名称 class TestDemo(unittest.TestCase):
直接使用Python的assert进行断言 # MyPytest.py import pytest def inc(x): return x+1 # 判断结果不等于5 def test_inc...为了对这种异常场景进行断言,我们需要使用pytest.raises,用法如下: # MyPytest.py import pytest def division(x): return 100/...(['MyPytest.py']) 注意:断言 type 的时候,异常类型是不需要加引号的,断言 value 值的时候需转 str 非预期内异常 如果我们不知道预期异常的是什么,我们可以使用match...(['MyPytest.py']) pytest-assume插件 pytest-assume是一个可以允许pytest测试用例中执行多个失败的断言的插件。..._02(): pytest.assume(inc(3) == 5) pytest.assume(inc(3) == 4) pytest.assume(inc(3) == 3)
运行的时候,使用 python 运行 run_all.py 来执行测试用例。...在命令行运行带上 -s 参数 > pytest -s 那么在 pytest.main() 里面等价于 import pytest# 带上-s参数 pytest.main(["-s"]) 在命令行运行带上多个参数时...> pytest -s -x 那么在 pytest.main() 里面等价于 import pytest# 带上-s参数 pytest.main(["-s", "-x"]) 指定运行某个用例 指定运行...‘’’ pytest cases/module1/test_x1.py ‘’’ 那么在 pytest.main() 里面等价于 import pytest# 运行指定py文件 pytest.main(...(["cases/module1/test_x1.py::test_x"]) 通过上面跟命令行运行的对比,对 pytest.main() 的使用也就基本掌握了 plugins参数的使用 一般我们写插件的代码放到
当我们想在每个测试方法之前运行一些代码时,将使用夹具。因此,我们定义夹具而不是在每个测试中都重复相同的代码。...通常,固定装置用于初始化数据库连接,传递基数等 通过将标记为 @ pytest.fixture 通过提及固定装置作为输入参数,测试方法可以使用固定装置。...使用以下代码创建一个新文件test_basic_fixture.py import pytest @pytest.fixture def supply_AA_BB_CC(): aa=25 bb...要对多个测试文件使用相同的fixture ,我们将在名为conftest.py的文件中创建灯具方法。 让我们通过以下示例进行查看。...使用以下代码创建3个文件conftest.py,test_basic_fixture.py,test_basic_fixture2.py conftest.py import pytest @pytest.fixture
# test_1.py import pytest @pytest.fixture def login(): print("输入账号,密码先登录") def test_1(login):...用例 1:登录之后其它动作 111 .用例 2:不需要登录,操作 222 ....方式二:usefixtures @pytest.fixture def login1(): print("输入账号,密码先登录") @pytest.fixture def login2():...print("please输入账号,密码先登录") @pytest.mark.usefixtures("login1", "login2") def test_s1(): print...每一个.py文件调用一次,该文件内又有多个function和class session 每个session只运行一次,在自动化测试时,登录步骤可以使用该session 范围:session > module
我们都知道,在做单元测试框架中有UnitTest和Pytest,前者是Python中自带无需安装,Pytest需要安装,今天我们来讲的就是Pytest,当然如果是做自动化,建议两个都要掌握一下,可以针对实际业务灵活使用...1、环境安装 pip insatll pytest 如果要解决安装满的问题,可以使用豆瓣源: pip install -i https://pypi.douban.com/simple pytest...查看安装是否成功和安装版本: pytest --version 2、基本使用 我们使用别人的框架,就得遵守别人的规则,所以pytest也有相应的规则: 1:用例文件:所有文件名为 test_...,就是这是main主函数入口,调用了pytest的main方法,方法里参数放在一个列表里 其他的我们需要什么参数就往里填就可以了 到了这里,基本使用我们就讲完了,实际看完感觉是不是也就那样,我们总结下,...本篇讲的pytest环境安装,pytest基本规则,pytest函数和类的使用,main函数运行方式 还有前置、后置、配置文件、参数化(数据驱动)、断言、重试机制、插件报告,不要着急慢慢来
pytest 框架里面的元数据可以使用 pytest-metadata 插件实现。...文档地址https://pypi.org/project/pytest-metadata/ pytest-metadata 环境准备 使用 pip 安装 pytest-metadata pip install...pytest-metadata 查看 pytest 元数据 使用pytest 执行用例的时候加上 -v 参数(或--verbose),在控制台输出报告的头部就会输出元数据(metadata) >pytest...比如当我们完成了一个项目,需要添加作者信息,于是就可以添加元数据 pytest —metadata auther yoyo 如果需要添加多个元数据,可以使用多次 --metadata 参数添加 pytest...hook函数 在代码里面也可以新增/修改/删除 元数据,我们可以使用 pytest_metadata hook函数 import pytest @pytest.mark.optionalhook def
1 简介 用例失败重跑可以使用插件pytest-rerunfailures来实现; pytest-rerunfailures有环境要求: Python 3.5-3.8, or PyPy3 pytest..., pytest-cov, pytest-forked, pytest-html, pytest-metadata, pytest-ordering, pytest-xdist C:\Users\Administrator...> 2 插件pytest-rerunfailures安装 pip install pytest-rerunfailures C:\Users\Administrator>pip install pytest-rerunfailures...reruns=n(重新运行次数) reruns_delay=m(等待运行秒数) 4 注意事项 不可以和fixture装饰器一起使用: @pytest.fixture() 该插件与pytest-xdist...3, reruns_delay=1) def test_login(): name = "zhang" assert name == "zhagnsan" 6 重新运行所有失败的用例 使用
Pytest执行用例规则 Pytest在命令行中支持多种方式来运行和选择测试用例 1.对某个目录下所有的用例 pytest 2.对模块中进行测试 pytest test_mod.py 3.对文件夹进行测试...pytest testing 4.通过标记来进行测试 pytest -m slow 这种方式会运行所有通过装饰器 @pytest.mark.slow进行装饰的测试用例。...--pyargs pkg.testing 这将会导入pkg.testing并使用其文件系统位置来查找和运行测试。...-q 简单打印,只打印测试用例的执行结果 pytest -q test_1.py 9.-s 详细打印 pytest -s test_1.py 10....-x 遇到错误时停止测试 pytest -x test_1.py 11.
2 pytest-repeat插件为了重复执行用例,我们可以使用pytest-repeat插件;详细参考官方教程:https://pypi.org/project/pytest-repeat/2.1 环境要求...2.2 插件安装pip3 install pytest-repeat图片3 pytest-repeat使用3.1 重复测试直到失败将pytest的 -x 选项与pytest-repeat结合使用,以强制测试运行程序在第一次失败时停止...# 作用:pytest-repeat插件的使用# 联系:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonimport randomimport...:test_pytest_repeat01.py# 作用:pytest-repeat插件的使用# 联系:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonimport....py# 作用:pytest-repeat插件的使用# 联系:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonimport pytestdef
简介: fixture区别于unnitest的传统单元测试(setup/teardown)有显著改进: 1.有独立的命名,并通过声明它们从测试函数、模块、类或整个项目中的使用来激活。 ...断言成功 . ========================== 1 passed in 0.17 seconds =========================== 使用多个fixture...========== Process finished with exit code 0 调用fixture的三种方法 1.函数或类里面方法直接传fixture的函数参数名称 注释:代码详见上图 2.使用装饰器...@pytest.mark.usefixtures()修饰需要运行的用例 # 作者 :admin import pytest @pytest.fixture(scope="class...============== Process finished with exit code 0 3.叠加usefixtures 如果一个方法或者一个class用例想要同时调用多个fixture,可以使用
该测试方法中的其余代码不会执行,并且pytest将继续使用下一个测试方法。 例子: assert“ hello” ==“ Hai”是断言失败。...允许我们使用pytest标记@ pytest.mark为测试方法设置各种属性。...要在测试文件中使用标记,我们需要在测试文件上导入pytest。 在这里,我们将不同的标记名称应用于测试方法,并根据标记名称运行特定的测试。...我们可以使用定义每个测试名称上的标记 @pytest.mark.. 我们在测试方法上定义了标记set1和set2,我们将使用标记名称来运行测试。...使用以下代码更新测试文件 test_sample1.py import pytest @pytest.mark.set1 def test_file1_method1(): x=5 y=6
2 文件格式 # 文件名为:pytest.ini [pytest] addopts = xfail_strict = 3 查看pytest.ini的选项 使用命令: pytest --help...# 作用:pytest.ini的使用 # 联系:VX(NoamaNelson) # 博客:https://blog.csdn.net/NoamaNelson import pytest @pytest.mark.name...: run the name old: run the old case: run the test_case 标记后可使用pytest --markers查看: (venv) F:\pytest_study...的使用 # 联系:VX(NoamaNelson) # 博客:https://blog.csdn.net/NoamaNelson import pytest @pytest.mark.name def...5.5 norecursedirs pytest 收集测试用例时,会递归遍历所有子目录; 如果有些目录不需要执行,可使用norecursedirs参数简化 pytest 的搜索工作; 方法如下,多个路径用空格隔开
1 使用场景当我们为了提高用例的复用性,会用到不同的fixture,比如登陆场景;但是如果登陆场景,我们使用不同的账号进行测试,那如何来做?...此时不能使用fixture把账号直接写死,需要通过传参的方式来实现。...文件名称:test_request.py# 作用:fixture传参# 联系:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonimport pytest...# 传一个参数@pytest.fixture()def user_login(request): user_name = request.param return user_namedata...# 传多个fixture@pytest.fixture()def user_name(request): name = request.param return name@pytest.fixture
2 使用方法@pytest.mark.自定义名称3 实例# -*- coding:utf-8 -*-# 作者:NoamaNelson# 日期:2022/11/18 # 文件名称:test_mark.py...# 作用:自定义标记mark的使用# 联系:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonimport pytest@pytest.mark.logindef...test_login(): print("用户登陆")@pytest.mark.case_adef test_case_a(): print("执行用例a")@pytest.mark.case_bdef...test_case_b(): print("执行用例b")@pytest.mark.quitdef test_quit(): print("用户退出")运行一个标记:pytest -s -...我们需要创建一个pytest.ini文件,加上自定义mark;另外,pytest.ini需要和运行的测试用例同一个目录,或在根目录下作用于全局;后边再详细学习pytest.ini,先看下本文如何避免警告
1 说明pytest中使用assert进行断言,和unittest是有区别的,后边详细列举;pytest中的assert后可以为表达式,为True表示用例通过。...()输出为:(venv) F:\pytest_study\test_case\test_d>pytest test_assert.py==================================...-6.2.4, py-1.10.0, pluggy-0.13.1rootdir: F:\pytest_study\test_case\test_dplugins: allure-pytest-2.8.12...========================= 2 passed in 0.31s ==============================================3.3 异常断言可以使用...是异常类型,用type从异常信息中获取;division by zero是异常的值,使用value从异常信息中获取。
本文共835字 阅读约需2分钟 一、前言 学习pytest时,总会习惯性的和unittest对比使用,自然就断言pytest和unittest也是有些区别的。...五、异常断言 结合pytest 断言特点和《Pytest之使用断言指定异常》,再来一个异常的案例,比如除数不能为零,示例代码如下: # 异常断言 # 异常断言 def test_zero_division...pytest.raises,来捕获对应的异常示例,exceptionInfo为一个对象示例来接收属性信息,通常是断言异常的 type 和 value 值 1、使用match断言 match 关键字是测试正则表达式与异常的字符串表示形式是否匹配...这种方法只能断言value,不能断言类型 示例代码如下: # match的使用 def test_zero_division_match(): with pytest.raises(ZeroDivisionError...2、检查断言装饰器使用 示例代码如下: # 断言装饰器 @pytest.mark.xfail(raises=ZeroDivisionError) def test_f(): 1 / 0 个人觉得这个断言装饰器
fixture功能 传入测试中的数据集 配置测试前系统的数据准备,即初始化数据 为批量测试提供数据源 fixture可以当做参数传入 如何使用 在函数上加个装饰器@pytest.fixture(),个人理解为...多个fixture的使用 示例代码如下: # -*- coding: utf-8 -*- # @Time : 2020/10/24 18:43 # @Author : longrong.lang...test_login(username, password): print('\n输入用户名:'+username) print('输入密码:'+password) print('登录成功...fixture的参数使用 示例代码如下: @pytest.fixture(scope="function", params=None, autouse=False, ids=None, name=None...') # 登录过程 def test_login(loginAction): print('\n点击登录进入系统') if __name__ == '__main__': pytest.main
pytest是一个非常成熟的全功能的Python测试框架 。文本记录该框架的使用方法。...等自动化测试、接口自动化测试(pytest+requests) pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html...集成 report框架----allure 也支持了pytest 官方文档: https://docs.pytest.org/en/latest/contents.html pytest 规范 pytest...安装 默认情况下 python 自带 pytest 框架,可以通过命令进行升级安装 pip install -U pytest 验证安装 pytest --version 输出 pytest 版本表明安装成功...C:\Users\Administrator>pytest --version pytest 6.2.5 运行方式 测试类主函数模式 可以在 python 中执行 pytest 命令 引入 pytest
领取专属 10元无门槛券
手把手带您无忧上云